C# Simple Image Resize : File Size Not Shrinking

后端 未结 6 955
夕颜
夕颜 2021-01-03 08:39

I have a question in regards to the code below. The code I have below successfully runs through a directory, and sets the resoultion of the picture to a smaller size. Howe

6条回答
  •  轮回少年
    2021-01-03 09:45

    Found the problem. Thanks @yetapb for showing a cleaner version of the code, but that still didn't work. The answer to the problem was that I needed to explicity specify the type of file type that the image would be saved as. My guess is that because I did not specify the image format explicitly, the image compression was not handled accordingly.. A Bitmap was just saved with a smaller resolution with a '.jpg' slapped onto it, and not compressed accordingly. The following code now works.

                files = System.IO.Directory.GetFiles(@"C:\PicFolder");
                for (string file in files)
                {
                Bitmap tempBmp = new Bitmap(file);
                Bitmap bmp = new Bitmap(tempBmp, 807, 605);
    
                bmp.Save(
                @"C:\NewPicFolder\Pic" + count + ".jpg",
                System.Drawing.Imaging.ImageFormat.Jpeg);
                count++;
                }
    

提交回复
热议问题