Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a thumbnail image

Status
Not open for further replies.
Joined
Jun 9, 2006
Messages
159
Location
US
Hello,

I have an image control on a web form. On page load I'd like to programatically set the source of the image (I guess using the Image.ImageUrl property) to a function that returns an Image, without first saving the image to the file system.

Any idea how I can do this?
 
Create a page that generates the thumbnail and set the ImageUrl to that page.

For instance:

Code:
myImage.ImageUrl = "~/Thumbnail.aspx?imageId=whatever";

Then in Thumbnail.aspx generate the thumbnail using GDI+ and write it to the response stream.

Code:
//page load in Thumbnail.aspx
string imageId = Request.QueryString["imageId"];

//load image into MemoryStream

//write MemoryStream to Response
Response.Write( /*image*/ );

There's more to it than that, but you get the idea.
 
Ok, thats a great way of doing it. I was hoping to keep all the functionality in one class, but I suppose this is as close as I can get.

Thanks alot!

-- shawn
 
here is some code I use to create thumbnails on the fly (when a user uses a dialog to upload)

Code:
                if (bResize == true)
                {
                    StrFileName = SaveThumbnail(strCompleteFilePath, Convert.ToInt32(NewImageHeight), Convert.ToInt32(NewImageWidth), iUserID, StrFileName);

                }

Code:
    private string SaveThumbnail(string strFilePath, int fHeight, int fWidth, int iUserID, string strFileName)
    {

        Bitmap myPic = new Bitmap(strFilePath);

        System.Drawing.Image myThumb = myPic.GetThumbnailImage(Convert.ToInt32(fWidth), Convert.ToInt32(fHeight), null, new IntPtr());

        string strNewPath = Server.MapPath(".\\images\\uploaded\\" + "UserID" + iUserID + "new" + strFileName);
        myThumb.Save(strNewPath, System.Drawing.Imaging.ImageFormat.Jpeg);

        return "new" + strFileName;


    }


Maybe you'll want to look at working with the object myThumb after the bitmap is loaded into it and see if that Image can be displayed. I don't see why it couldn't. Let me know if you try this and how it goes.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top