Click here to Skip to main content
16,015,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
what is the difference?

Bitmap newImage = new Bitmap(_currentBitmap, newWidth, newHeight);
return newImag;

AND

Bitmap animage= new Bitmap( newWidth, newHeight);
using (Graphics gr = Graphics.FromImage(animage))
{
gr.SmoothingMode = SmoothingMode.HighQuality;
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
gr.CompositingQuality = CompositingQuality.HighQuality;
gr.DrawImage(_currentBitmap, new Rectangle(0, 0, newWidth, newHeight));
}
return animage;
Posted
Updated 9-Apr-20 19:47pm

Almost nothing, here is the source code of the System.Drawing.Bitmap(Image, int, int) in the .NET framework:

C#
public Bitmap(Image original, int width, int height) : this(width, height) {
            Graphics g = null;
            try {
                g = Graphics.FromImage(this);
                g.Clear(Color.Transparent);
                g.DrawImage(original, 0, 0, width, height);
            }
            finally {
                if (g != null) {
                    g.Dispose();
                }
            }
        }
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 4-Sep-13 23:25pm    
Good info, my 5, but there is essential difference: 1) scaling quality, 2) the code you show is not guaranteed. Please see my answer.
—SA
Ron Beyer 5-Sep-13 0:03am    
Yup, the interface won't change (may depreciate) but the implementation is not guaranteed to remain the same. Other than setting some GDI flags its almost the same process, but setting the flags does give predictable quality.
Look at the documentation: http://msdn.microsoft.com/en-us/library/334ey5b7.aspx[^].

As you can see, the documentation on this constructor does not specify how the image is scaled, which what interpolation quality. In your second code sample, you can specify it. You can play with the second code sample and figure out how exactly scaling is done (as you can compare final results pixel by pixel). However, you cannot rely on those results: as the fact this detail is not described in documentation, the result may depend on some unknown factors, such as version of the .NET framework. I would always use the second variant of the code, as soon as image quality is important.

—SA
 
Share this answer
 
Comments
Ron Beyer 5-Sep-13 0:01am    
Yup, +5
Sergey Alexandrovich Kryukov 5-Sep-13 0:12am    
Thank you, Ron.
—SA
I thought I would add to this. If you are looking to scale a bitmap, try this:
var bmp = new Bitmap(newWidth, newHeight);
using var g = Graphics.FromImage(bmp);
g.SmoothingMode = SmoothingMode.None;
g.InterpolationMode = InterpolationMode.NearestNeighbor;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.CompositingQuality = CompositingQuality.AssumeLinear;
g.DrawImage(Image, new Rectangle(0, 0, newWidth, newHeight));
return bmp;

Make sure to set the interpolation mode to Nearest neighbor, remove smoothing, and high quality pixel offset. Assume it is a linear scale. For best results, make the new width/height an integer multiple of the original.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900