Re: Resizing Images



On Tue, 31 Mar 2009 12:09:49 -0700, <dj1072@xxxxxxxxx> wrote:

Pete, thank you so much! I didn't know about the HighQualityBicubic
option which seems to have done the trick. My .NET image now looks
like an image saved directly from a desktop graphics program!

Glad it helped. :)

If anyone is interested, below is the code I ended up using to resize
a large image and save it out as a small image in a different
location.

Some comments about your code:

-- it is more idiomatic to use the Size struct, instead of keeping individual width/height variables. Doing so makes some of the later Graphics calls more convenient too.

-- your size calculations are wrong, because they don't take into account differing aspect ratios for input and output. For example, if your "max" dimensions are 400x300, but you are provided an image that's 400x400, you'll get output that's 400x400, exceeding the maximum height specified by 100 pixels. Instead of checking for a specific aspect ratio type (i.e portrait vs landscape, as your code does), you should simply calculate the scaling required for fitting either the width or height, and choose whichever is smaller. For example:

float scaleWidth = (float)iMaxWidth / iWidth,
scaleHeight = (float)iMaxHeight / iHeight;

float scaleOutput = Math.Min(scaleWidth, scaleHeight);

iNewWidth = scaleOutput * iWidth;
iNewHeight = scaleOutput * iHeight;

(There are other approaches you can take...for example, I have some applications where I want to flip the output sizes to match aspect ratio, to minimize the amount of "lost space" in the output image. But the above seems most directly a solution to the problem you _seem_ to be trying to solve).

-- There's no reason to use the Decimal type. It has extra overhead for no useful benefit in this application. You might as well just use regular floating point (e.g. "float", as I've done above). Also, whether you use Decimal or Float, you only need to cast one of the operands in the calculation; the other will be promoted automatically (I wouldn't even have bothered to mention these two, they're so insignificant, except I was writing stuff anyway :) ).

-- In your call to DrawImage(), there's not any point in specifying the "source rect", since you're drawing the entire thing. Just use the (Image, Rectangle) overload, or even the (Image, int, int, int, int) overload (since you're not bothering to store the values in a Size in the first place, why bother creating a Rectangle too?)

-- It is also idiomatic to use the "using" statement when dealing with IDisposable objects. Doing so is also a good way to avoid mistakes such as disposing objects in the wrong order (such as disposing a Bitmap instance before you've disposed the Graphics instance that draws into that Bitmap instance, as you've done here). :)

-- Finally, off the top of my head, my recollection is that specifying both the "quality" and "compression" for JPEG is superfluous. I don't remember if "compression" is simply ignored altogether, or is just the inverse of "quality", but either way I have found that specifying "quality" alone is sufficient.

Hope that helps.

Pete
.



Relevant Pages

  • Re: /boot/loader graphics support & extensibility
    ... They have VGA compatible hardware. ... Therefore all graphics functions must ... void gfx_setrgb(int color, int red, int green, int blue); ... x resolution, y resoluton, depth. ...
    (freebsd-hackers)
  • Re: MiniPix ProDOS and DOS33 Disks 1-3 ALL
    ... Purpose: Legacy Graphics Converter ... This program converts Apple IIe format Beagle Bros. ... Minipix size. ... int minihead = OLD_HEADER; ...
    (comp.sys.apple2.programmer)
  • Re: gif to binary data
    ... for graphics i thougt of converting the gif file to binary data so did ... int ig,jg; ...
    (comp.arch.embedded)
  • Re: Printing Document
    ... You need to get the DPIX returned by GetDeviceCaps not by the Graphics. ... public static void GetHardMargins(int hDc, ref float left, ref float top, ... > ''' The device's physical margins in 0.001 inch units. ... > Public Shared Function GetPhysicalMargins(_ ...
    (microsoft.public.dotnet.languages.vb)
  • Reply to Morten Wennevik: Drawing problem
    ... Graphics OffScreenGraphics = Graphics.FromImage; ... private void Main_Form_MouseDown(object sender, ... private void PaintToScreen1(int x, int y) ... int width = MouseX - startatX; ...
    (microsoft.public.dotnet.languages.csharp)

Quantcast