C# .NET - how to convert System.Drawing.Image to System.Web.UI.WebControls.Image

Asked By pankaj singh on 04-Oct-12 04:39 AM
hi expert
  how to convert  System.Drawing.Image to System.Web.UI.WebControls.Image
Tejaswini Prashant J replied to pankaj singh on 04-Oct-12 04:54 AM
Check following links..
Hope that will help

http://forums.asp.net/t/1822833.aspx/1
http://forums.asp.net/t/1661152.aspx/1

Manish Gupta replied to pankaj singh on 04-Oct-12 09:49 AM
try this

System.Web.UI.WebControls.Image objImage = Page.ResolveUrl("~/") + "ImageGenerator.aspx?path=" + Server.MapPath(picturefilename.ToString());

now in ImageGenerator.aspx.cs


 protected void Page_Load(object sender, EventArgs e)
    {

      string fileName = Request.QueryString["path"];
      System.Drawing.Image resizedImage = System.Drawing.Image.FromFile(fileName);
        
   
      // make a memory stream to work with the image bytes
      MemoryStream imageStream = new MemoryStream();

      // put the image into the memory stream
      resizedImage.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg);

      // make byte array the same size as the image
      byte[] imageContent = new Byte[imageStream.Length];

      // rewind the memory stream
      imageStream.Position = 0;

      // load the byte array with the image
      imageStream.Read(imageContent, 0, (int)imageStream.Length);

      // return byte array to caller with image type
      Response.ContentType = "image/jpeg";
      Response.BinaryWrite(imageContent);

    }
pankaj singh replied to Manish Gupta on 04-Oct-12 09:57 AM
This is good one.
Thank's
keep it up