3

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. However, the file size is not changed. For example, an image with dimensions of 2400x1800 with file size of 1.5MB will be scaled to 800x600, but the 800x600 picture will still be 1.5MB file size. I'm think I may have to explicitly compress the picture, but I'm not sure. Any ideas?

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)
            {
                System.Drawing.Bitmap bmp = System.Drawing.Bipmap.FromFile(file);

                ResizeBitmap(bmp, 807, 605).Save(
                     @"C:\users\..\..\TempPicHold\Pic" + count.ToString() + ".jpg");
                count++;
            }
        }
        public Bitmap ResizeBitmap(Bitmap b, int nWidth, int nHeight)
        {
            Bitmap result = new Bitmap(nWidth, nHeight);
            using (Graphics g = Graphics.FromImage((Image)result))
                g.DrawImage(b, 0, 0, nWidth, nHeight);
            return result;
        }
contactmatt
  • 18,116
  • 40
  • 128
  • 186

6 Answers6

8

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++;
            }
contactmatt
  • 18,116
  • 40
  • 128
  • 186
  • 2
    Also, check out the [image resizing pitfalls](http://nathanaeljones.com/163/20-image-resizing-pitfalls/) you need to avoid. Right now you are leaking GDI handles, memory, and producing low-quality images. – Lilith River Jul 16 '11 at 18:38
  • Actually, why not use a library that avoids all the GDI bugs you are running into? The http://imageresizing.net library is free, open-source, supported, and well documented. And it has a 1-line API that takes about about a second to understand. – Lilith River Jul 16 '11 at 18:40
2

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

GrayWizardx
  • 19,561
  • 2
  • 30
  • 43
2

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.

Dan
  • 17,375
  • 3
  • 36
  • 39
  • Also: `graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;` – vapcguy Mar 12 '15 at 22:09
0
 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++;
            }  

}

Fadi
  • 2,320
  • 8
  • 38
  • 77
0

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

Broam
  • 4,602
  • 1
  • 23
  • 38
0

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++;   
    }
}

}

µBio
  • 10,668
  • 6
  • 38
  • 56
  • So instead of doing `System.Drawing.Bitmap bmp = System.Drawing.Bipmap.FromFile(file);`, you create a new Bitmap? This version also still has the issue of slapping a `.jpg` at the end of a bitmap file, though. OP seemed to want to convert, not just rename as a `.jpg`. – vapcguy Mar 09 '15 at 23:17
  • Looking at what I wrote and what the OP wrote (6 years ago!) I can see I just edited his code to a point it reduced file sizes (for me). His question never explicitly states wanting to convert to jpeg, but his answer to his own question does. – µBio Mar 10 '15 at 16:42