Image Resizing.

T

tshad

I have a class I created to resize images in my program.

It seems to work pretty good with JPGs and GIFs. Pngs look pretty good
until you try to expand them, such as in Micorsoft Office Picture Manager by
holding down the ctrl key and rolling your mouse wheel. It expands fine and
you can see the picture but not the detail. The letters start to blur. If
you do the same thing with the original PNG file, it looks great as you
increase it.

How can I handle compression with a PNG file?

Here is my code:
***************************************************************
public static Image GetImageResize(Image oImage, int width)

{

int imageHeight;

int imageWidth;

float imageRatio;



// string s = System.Text.Encoding.ASCII.GetString(arrBytes);

imageRatio = (float)oImage.Height / (float)oImage.Width;

imageHeight = (int)(width * imageRatio);

imageWidth = width;

oImage = oImage.GetThumbnailImage(imageWidth, imageHeight, null,
IntPtr.Zero);

return oImage;

}

***************************************************************

Thanks,

Tom
 
T

tshad

Peter Duniho said:
I have a class I created to resize images in my program.

It seems to work pretty good with JPGs and GIFs. Pngs look pretty good
until you try to expand them, such as in Micorsoft Office Picture
Manager by
holding down the ctrl key and rolling your mouse wheel. [...]

You haven't provided enough detail as to the nature of the images you're
using for anyone to know for sure what's going on.

However, I suspect that the fundamental issue is that you're using a
method intended for generating thumbnails as a general-purpose resizing
technique. The docs for GetThumbnailImage() specifically warn against
this, and I suspect that what's happening is that your PNG files have an
embedded thumbnail image or GetThumbnailImage() is otherwise able to
obtain a pre-rendered thumbnail image that it then uses to generate the
image of the specific size you ask for.

Since a thumbnail is going to necessarily have less detail than the
original, any time this happens, the resized image is also going to have
less detail.

That makes sense.

The size of the original image is very large so when I increase the size of
that file, it is already large so I don't lose any data. But when I try to
increase the size of the compressed (resized image), I already have lost
detail so the increase size will lose more data.
You should be, just as the docs for GetThumbnailImage() indicate, be
resizing the image directly.

I am.

I get oImage:
System.Drawing.Image oImage = null;

oImage = oImage.GetThumbnailImage(imageWidth, imageHeight, null,

from doing:

MemoryStream ms = new MemoryStream(myByteArray, 0, myByteArray.Length);

ms.Write(myByteArray, 0, myByteArray.Length);

oImage = Image.FromStream(ms, true);
You do this conveniently just by using the Image() constructor that takes
an original image and a new size,

Isn't that what I am doing?
or if you need higher-quality scaling, you can create a new Bitmap
instance of the correct size
and draw your original image into that Bitmap at the desired size,
setting the Graphics settings as you desire (the InterpolationMode and
SmoothingMode properties are the ones people usually want control over in
this case).


How would I do that?

I also found that if I do the:

oImage = Image.FromStream(ms, true);


with a .pcx file, I get an error:

Parameter is not valid.

Not sure why, but I just don't compress that file.

Thanks,

Tom
 
T

tshad

Peter said:
[...]
You should be, just as the docs for GetThumbnailImage() indicate, be
resizing the image directly.

I am.

I don't think you are. By "directly" I mean that you shouldn't be
using GetThumbnailImage() at all.
[...]
You do this conveniently just by using the Image() constructor that
takes
an original image and a new size,

Isn't that the same thing as the GetThumbnailImage() where you take the
Image and GetThumbnailImage resizes it to the size you pass:
MemoryStream ms = new MemoryStream(myByteArray, 0, myByteArray.Length);
ms.Write(myByteArray, 0, myByteArray.Length);
System.Drawing.Image oImage = Image.FromStream(ms, true);
oImage = oImage.GetThumbnailImage(imageWidth, imageHeight, null,
IntPtr.Zero);

as opposed to your GetImageResize:

MemoryStream ms = new MemoryStream(myByteArray, 0, myByteArray.Length);
ms.Write(myByteArray, 0, myByteArray.Length);
System.Drawing.Image oImage = Image.FromStream(ms, true);
oImage = GetImageResize(Image image, imageWidth, imageHeight;

I would assume (though I am probably wrong here) that the Bitmap resize is
doing the same thing as the GetThumbnailImage.
I haven't seen any code in this thread that uses an Bitmap()
constructor explicitly (sorry...I should have stated "Bitmap" instead
of "Image"...the Image class is abstract and has no constructors of
course). For example:
Image GetImageResize(Image image, int cxWidth, int cyHeight)
{
return new Bitmap(image, cxWidth, cyHeight);
}
But a BitMap takes more space that say a jpeg.

What we are trying to do is take a 1 meg jpeg file and compress (may be the
wrong word here) it a to a newer size based on the width. Using
GetThumbnailImage we get a new jpeg to about 50k.
For example:

Image GetImageResize(Image image, int cxWidth, int cyHeight)
{
Image imageRet = new Bitmap(cxWidth, cyHeight);

using (Graphics gfx = Graphics.FromImage(imageRet))
{
gfx.DrawImage(image, 0, 0, cxWidth, cyHeight);
}

return imageRet;
}

I assume you meant:

Image imageRet = new Bitmap(image,cxWidth, cyHeight);
Adding, of course, whatever specific drawing settings you want in
order to control the quality of the redrawn image.


Well, according to the docs for the Bitmap class, .NET/GDI+ doesn't
support PCX. It lists BMP, GIF, EXIG, JPG, PNG and TIFF. No PCX.


And you explained that the Image class is really an abstract class and I
assume is derived from the Bitmap class which would explain why it doesn't
work.

Thanks,

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top