?
Solved

Why in a resize process, the System.Drawing.Bitmap.Save method create a larger file than original??

Posted on 2008-09-29
3
Medium Priority
?
455 Views
Last Modified: 2013-11-07
I'm doing a resize function in c#, when I resize a file image with sizes: 2832x2128 weights 1.62MB, the result of doing Bitmap.Save is a file with sizes: 1024x768px, but the weight of the new file is 1.74MB, which i don't understand why happens.

the code is the following:
public System.Drawing.Bitmap CopyBitmap(Bitmap source, Rectangle part)
        {
            Bitmap bmp = new Bitmap(part.Width, part.Height, source.PixelFormat);
            Graphics g = Graphics.FromImage(bmp);
            g.DrawImage(source, part.X, part.Y, part.Width, part.Height);
            g.Dispose();
            return bmp;
        }
 
 
public void redim()
{
Bitmap bmpOrig = new Bitmap("c:\\original.jpg"); // file 2832x2128, 1.62MB
 
            int newWidth = bmpOrig.Width;
            int newHeight = bmpOrig.Height;
            float relacionAspecto = 0;
 
            if (bmpOrig.Width > bmpOrig.Height)
            {
 
                newWidth = 1024;
                relacionAspecto = (float)((float)bmpOrig.Height / bmpOrig.Width);
                newHeight = (int)Math.Round(newWidth * relacionAspecto);
            }
            else
            {
 
                newHeight = 1024;
                relacionAspecto = (float)((float)bmpOrig.Width / bmpOrig.Height);
                newWidth = (int)Math.Round(newHeight * relacionAspecto);
            }
 
 
            Bitmap nueva = CopyBitmap(bmpOrig, new Rectangle(0, 0, newWidth, newHeight));
 
            bmpOrig.Dispose();
 
            nueva.Save(@"C:\resize.jpg"); // file with 1024x768, 1.74MB
 
            nueva.Dispose();
}

Open in new window

0
Comment
Question by:DavinchiSAT
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 22597885
It's because you are NOT saving the new image as a JPG!

    nueva.Save(@"C:\resize.jpg"); // file with 1024x768, 1.74MB

What your code above actually does is create a Bitmap file with a ".jpg" extension.  =\

You need to use the SECOND parameter to Bitmap.Save() to specify the JPEG format:
http://msdn.microsoft.com/en-us/library/9t4syfhh.aspx

    nueva.Save(@"c:\resize.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
0
 
LVL 3

Expert Comment

by:Diveblue
ID: 22597895
Since you created the bitmap from a file, the image was decompressed using whatever format compressed the file to begin with.  Now you can use the overload() of BitMap.Save() to specify an ImageFormat property that changes the image type from JPEG to GIF or TIFF or PNG or BMP, etc.  Using the overload of BitMap.Save that takes Codec arguments may allow you greater control over the specific compression algorithm used to save the file.

0
 

Author Comment

by:DavinchiSAT
ID: 22597929
it works!!!!!!!!!!!!!!!!!!!,

tnks!!!!

got the points!!
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question