简体   繁体   English

从一个目录向下缩放.JPG图像,然后另存为.JPG / .PNG格式

[英]Scaling .JPG images down from one directory and saving to another as .JPG/.PNG

SO what I'm trying to do here is to import a list of .JPG images that are of a large size and I would like to scale them down without too much of a quality loss and then output them as a .JPG/.PNG to not take up too much memory like .BMP's do. 因此,我要在此处执行的操作是导入大尺寸的.JPG图像列表,我希望将其按比例缩小而不会造成太多质量损失,然后将其输出为.JPG / .PNG不会像.BMP那样占用太多内存。

I know that you can only manipulate images when they are .bmp's. 我知道您只能在图像为.bmp时进行操作。 Here's some of the sample code I have(I only know how to import them) 这是我的一些示例代码(我只知道如何导入它们)

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
}

if there is anything else you would like to know post below and I'll edit the question 如果您还有其他想知道的信息,我将编辑问题

Try this: 尝试这个:

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");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM