Forum Moderators: open

Message Too Old, No Replies

c# Image Resize of Existing Images

         

IntegrityWebDev

4:35 pm on Nov 29, 2010 (gmt 0)

10+ Year Member



Hello all, this is a new area for me. I have inherited some code that I'm trying to improve upon.

basically I have 3 folders on the web server under site.com/images: large, regular, thumb

In each folder is the same image: image.jpg (this will sometimes be image.png) and all three images are always going to be 500x500 pixels.

In the regular folder I need to make the image 200x200
In the thumb folder I need to make the image 80x80

I haven't found a good, clear tutorial on how to resize an existing image on the server.

Thanks for any help!
Chris

Ocean10000

2:39 am on Nov 30, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I just copied and pasted this from a working site writen in C# using basic asp.net controls. I have put a few comments in to help make things a bit clearer.

protected void btn_Upload_Click(object sender, EventArgs e)
{
if (this.FileUpload1.PostedFile.ContentLength <= 0)
{
return;
}

//Gets the next valid ID from the database
Ocean2.Web.Dal.Member.Comic_Images Image = new Ocean2.Web.Dal.Member.Comic_Images();

Image.Comicid = this.ComicId;
Image.Name = this.FileUpload1.PostedFile.FileName;
Image = Ocean2.Web.Dal.Member.Comic_Images.Insert(Image);
if (Image.Id <= 0)
{
return;
}

//Sets up the call back for the thumbnail code
System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);


//Just creates the file path to save the files
string Path = System.Configuration.ConfigurationSettings.AppSettings["ComicImages"];
string ComicImagepath = string.Format(Path + "ComicImage{1}_{0}", Image.Id, Image.Comicid);

//Takes the original image and puts it into a bitmap class (I usually only work with png images so this is safe for me)
System.Drawing.Bitmap ComicImage = new System.Drawing.Bitmap(this.FileUpload1.PostedFile.InputStream);

//creates two new file streams for the different sizes
System.IO.FileStream MediumStream = new System.IO.FileStream(ComicImagepath + "_2.jpg", System.IO.FileMode.CreateNew);
System.IO.FileStream ThumbStream = new System.IO.FileStream(ComicImagepath + "_3.jpg", System.IO.FileMode.CreateNew);

//creates the thumbnails at the two smaller sizes.
System.Drawing.Image MediumImage = ComicImage.GetThumbnailImage(230, 400, myCallback, IntPtr.Zero);
System.Drawing.Image ThumbImage = ComicImage.GetThumbnailImage(115, 200, myCallback, IntPtr.Zero);

//saves and closes the files and disposes of any objects
//these class's last time I checked do use GDI objects so always make sure to dispose of them properly.
MediumImage.Save(MediumStream, System.Drawing.Imaging.ImageFormat.Jpeg);
MediumStream.Close();
ThumbImage.Save(ThumbStream, System.Drawing.Imaging.ImageFormat.Jpeg);
ThumbStream.Close();
ThumbImage.Dispose();
MediumImage.Dispose();
this.Response.Redirect("example.aspx?id=" + ComicId);
}

public bool ThumbnailCallback()
{
return false;
}

Reference
Image.GetThumbnailImage Method [msdn.microsoft.com]

IntegrityWebDev

1:33 pm on Nov 30, 2010 (gmt 0)

10+ Year Member



Excellent, going to study this and put it to good use. Thanks!