C# Simple Image Resize : File Size Not Shrinking

后端 未结 6 928
夕颜
夕颜 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:24

    Not sure about bitmaps, but for other images you can specify a different compression encoder. MSDN details here

    0 讨论(0)
  • 2021-01-03 09:27

    You need to set some of the properties on the Graphics object to change the quality of the image.

    graphics.CompositingQuality = CompositingQuality.HighSpeed; 
    graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
    graphics.CompositingMode = CompositingMode.SourceCopy;
    graphics.DrawImage(photo, 0, 0, width, height);
    

    You can also set different compression encodings when saving the file or save it in a different format.

    0 讨论(0)
  • 2021-01-03 09:27
     private void button4_Click(object sender, EventArgs e)
      {
                String[] files;
                int count = 0;
                files = System.IO.Directory.GetFiles(@"C:/dataset");
                foreach (string file in files)
                {
                Bitmap tempBmp = new Bitmap(file);
                Bitmap bmp = new Bitmap(tempBmp, 200, 200);
    
                bmp.Save(
                @"C:/Newdataset1/" + count + ".jpg",
                System.Drawing.Imaging.ImageFormat.Jpeg);
                count++;
                }  
    

    }

    0 讨论(0)
  • 2021-01-03 09:44

    Interesting implementation detail: flip the image twice, and it will cause the thumbnail to be thrown out and this will decrease the file size.

    result.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone); result.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);

    0 讨论(0)
  • 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++;
                }
    
    0 讨论(0)
  • 2021-01-03 09:46

    Made a couple changes, the following code reduced file sizes as expected (for me).

    private void Form1_Load(object sender, EventArgs e)
    {
        string[] files = null;
        int count = 0;
        files = System.IO.Directory.GetFiles(@"C:\Users\..\..\ChristmasPicsResized");
        foreach (string file in files)
        {
            Bitmap bmp = new Bitmap( file );
            new Bitmap( bmp, 807, 605 ).Save(
                       @"C:\users\..\..\TempPicHold\Pic" + count.ToString() + ".jpg");
            count++;   
        }
    }
    

    }

    0 讨论(0)
提交回复
热议问题