【问题标题】:Scaling .JPG images down from one directory and saving to another as .JPG/.PNG将 .JPG 图像从一个目录缩小并以 .JPG/.PNG 格式保存到另一个目录
【发布时间】:2013-07-07 18:26:49
【问题描述】:

所以我在这里要做的是导入一个大尺寸的 .JPG 图像列表,我想在没有太多质量损失的情况下缩小它们,然后将它们输出为 .JPG /.PNG 不会像 .BMP 那样占用太多内存。

我知道您只能处理 .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
}

如果您还有什么想知道的,请在下面发帖,我会编辑问题

【问题讨论】:

    标签: c# image scaling


    【解决方案1】:

    试试这个:

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

    【讨论】:

      猜你喜欢
      • 2020-05-01
      • 1970-01-01
      • 2021-09-22
      • 1970-01-01
      • 1970-01-01
      • 2018-04-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多