resizing images


M

Mike P

I am displaying images from a database using the code below.

Is there any way I can resize the images before rendering them?



public void showSelectedImage(string strEncodedImage)
{
Response.Clear();
Response.ContentType = "image/jpeg";

System.Drawing.Image image = retrieveImage(strEncodedImage);
image.Save(Response.OutputStream,
System.Drawing.Imaging.ImageFormat.Jpeg);
}

private System.Drawing.Image retrieveImage(string strEncodedImage)
{
System.Drawing.Image image = null;

byte[] imageData = Convert.FromBase64String(strEncodedImage);

MemoryStream memStream = new MemoryStream(imageData);
image = System.Drawing.Image.FromStream(memStream);

return image;
}
 
Ad

Advertisements

J

Jeff Johnson

I am displaying images from a database using the code below.

Is there any way I can resize the images before rendering them?

Sure. Create a new Bitmap of the desired size, get a Graphics object via the
static Graphics.FromImage(<your new Bitmap>) method, and then use one of the
DrawImage() overloads on that object to draw the image to your new Bitmap.
 
M

Mike P

Jeff,

I have tried using the following code, but the image does not display on
screen :

public void showSelectedImage(string strEncodedImage)
{
Response.Clear();
Response.ContentType = "image/jpeg";

System.Drawing.Image image = retrieveImage(strEncodedImage);

Bitmap bmp = new Bitmap(600, 350);

Graphics grp = Graphics.FromImage(bmp);

grp.DrawImage(image, 0,0);

grp.Save();




//image.Save(Response.OutputStream,
System.Drawing.Imaging.ImageFormat.Jpeg);
}
 
R

Roger Frost

Which screen? I don't see a screen object in this code. Where does
Response go after showSelectedImage() returns?

It's hard to tell without a complete example.

At the very least, that last line image.save() that is commented out should
be bmp.Save() since bmp is your newly resized image that you're drawing to,
and is required to get the data into the stream.

Also, for the resizing to be correct you will need to use a
graphics.DrawImage() overload that specifies a point and a size (ie. a
rectangle). Naturally your point will be 0,0 and your size will be equal to
bmp.Width, bmp.Height (600, 350).
 
J

Jeff Johnson

In addition to what Roger said, anything graphics-related should be disposed
of, so your
code ought to look more like

using (Bitmap bmp = new Bitmap(600, 350))
using (Graphics grp = Graphics.FromImage(bmp))
{
...
}
 
P

Peter Duniho

I am displaying images from a database using the code below.

Is there any way I can resize the images before rendering them?

The easiest way to resize an image is simply to pass the Image to one of
the Bitmap constructors that has an Image as the first argument and which
allows you to specify the size.

To use the higher-quality (and slower) interpolation modes in .NET, you'll
have to use the approach Jeff suggests. There are several threads in this
newsgroup were specific techniques and code have been posted; Google
Groups should be able to show those to you.

Pete
 
Ad

Advertisements

J

Jeff Johnson

The easiest way to resize an image is simply to pass the Image to one of
the Bitmap constructors that has an Image as the first argument and which
allows you to specify the size.

Argh, I keep forgetting about that....
 

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