2013-07-07 80 views
0

因此,我在這裏要做的是導入一個大尺寸的.JPG圖像列表,我想將它們縮小而沒有太多的質量損失,然後將它們輸出爲.JPG/.PNG以避免像.BMP那樣佔用太多內存。從一個目錄縮小.JPG圖像並保存爲另一個.JPG/.PNG

我知道你只能在處理.bmp的時候處理圖像。 下面是一些示例代碼的我已(我只知道如何導入它們)

private void LoadImages() 
{ 
    for (int i = 0; i < trees.Length; i++) 
    { 
     string d = imagePath + trees[i].latinName + ".JPG"; 
     treesImage[i] = Image.FromFile(d); 
    } 
    //the image path is a constant 
    //trees[i].latinName is a string property 
    //treesImage is an array of Images created. 
    //so I'd like(preferably within my for loop to create the .bmp's and scale down 
    //using a const value such as const int width = 400, const int height = 300; 
    //and I'd lke to save the image to a different diretory than imagePath 
} 

如果有其他任何你想知道的後下方,我將修改的問題

回答

0

試試這個:

int newWidth = 75; 
int newHeight = 50; 
for (int i = 0; i < trees.Length; i++) 
{ 
    string d = imagePath + trees[i].latinName + ".JPG"; 
    Image image = Image.FromFile(d); 

    //new image with size you need 
    Bitmap smallImage = new Bitmap(newWidth, newHeight); 
    Graphics g = Graphics.FromImage(smallImage); 

    //draw scaled old image to new with correct size 
    //g.drawImage(Image sourceImage, Rectangle destination, Rectangle source, GraphicsUnit gu) 
    g.DrawImage(image, new Rectangle(0, 0, smallImage.Width, smallImage.Height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel); 

    //format of new image is defined by it's name (.png/.jpg) 
    smallImage.Save(trees[i].latinName + "_new.png"); 
} 
相關問題