Resize Image

S

shapper

Hello,

I need to resize an image contained on a
System.Web.HttpPostedFileBase.

I would like to keep the same format. I have the following:

public Stream Resize(HttpPostedFileBase image, Int32 height, Int32
width) {

using (Image source = Image.FromStream(image.InputStream, true,
true)) {
using (Bitmap resized = (Bitmap)source.GetThumbnailImage(100,
50, null, IntPtr.Zero)) {
MemoryStream output = new MemoryStream();
resized.Save(output, source.RawFormat);
return output;
}
}

} // Resize

Is this the best way to do this?

I am also using RawFormat to maintain the format.

Thank You,

Miguel
 
P

Peter Duniho

shapper said:
Hello,

I need to resize an image contained on a
System.Web.HttpPostedFileBase.

I would like to keep the same format. I have the following:

public Stream Resize(HttpPostedFileBase image, Int32 height, Int32
width) {

using (Image source = Image.FromStream(image.InputStream, true,
true)) {
using (Bitmap resized = (Bitmap)source.GetThumbnailImage(100,
50, null, IntPtr.Zero)) {
MemoryStream output = new MemoryStream();
resized.Save(output, source.RawFormat);
return output;
}
}

} // Resize

Is this the best way to do this?

Define "best". "Best" according to what criteria?

I would personally not use GetThumbailImage() for general-purpose
resizing. If you really are only making thumbnails and want the output
to be tied to the Windows thumbnail data that may or may not exist for
an image, you can use it (though in that case you should rename your
method to make it clear that it's specifically for thumbnails, not
general-purpose). Otherwise, do the resizing explicitly yourself.

As for how specifically to do that, there are a number of techniques
possible. But if you don't need control over the specific interpolation
and quality of the resizing, you can just pass the original Image to the
Bitmap constructor that takes an Image and dimensions as arguments.
That would be the simplest.

For more details, just do a Google Groups search on this newsgroup for
the topic. There have been lots of previous discussions about this very
thing.

Pete
 
S

shapper

Define "best".  "Best" according to what criteria?

I would personally not use GetThumbailImage() for general-purpose
resizing.  If you really are only making thumbnails and want the output
to be tied to the Windows thumbnail data that may or may not exist for
an image, you can use it (though in that case you should rename your
method to make it clear that it's specifically for thumbnails, not
general-purpose).  Otherwise, do the resizing explicitly yourself.

As for how specifically to do that, there are a number of techniques
possible.  But if you don't need control over the specific interpolation
and quality of the resizing, you can just pass the original Image to the
Bitmap constructor that takes an Image and dimensions as arguments.
That would be the simplest.

For more details, just do a Google Groups search on this newsgroup for
the topic.  There have been lots of previous discussions about this very
thing.

Pete

Thank you Pete for the tips. I came up with the following:

public Image Resize(Image image, Int32 width, Int32 height,
InterpolationMode mode = InterpolationMode.HighQualityBicubic) {

Bitmap bitmap = new Bitmap(width, height);
Graphics graphics = Graphics.FromImage((Image)bitmap);
graphics.InterpolationMode = mode;
graphics.DrawImage(image, 0, 0, width, height);
graphics.Dispose();
return (Image)bitmap;

} // Resize
 

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