locked
Converting from Graphics or Bitmap back to Image RRS feed

  • Question

  • User-1397778012 posted

    Is there a way to set an Bitmap object back to an Image object in .net?

    Tuesday, July 6, 2010 1:08 PM

Answers

  • User-474337052 posted
    Image originalImage = Image.FromFile(fromPath);
            var thumbnailBitmap = new Bitmap(originalImage,(int)newImageWidth, (int)newImageHeight);
            using (Graphics g = Graphics.FromImage(thumbnailBitmap))
            {
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                g.DrawImage(thumbnailBitmap, 0, 0, (int)newImageWidth, (int)newImageHeight);
                g.Dispose(); originalImage.Dispose(); GC.Collect();
            }
    
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 6, 2010 3:20 PM

All replies

  • User-474337052 posted
     Bitmap bmp = new Bitmap("file");
            bmp.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
    
    Tuesday, July 6, 2010 2:32 PM
  • User-1397778012 posted


    Image originalImage = Image.FromFile(fromPath);
    var thumbnailBitmap = new Bitmap((int)newImageWidth, (int)newImageHeight);
    var thumbnailGraph = Graphics.FromImage(thumbnailBitmap);
    var imageRectangle = new Rectangle(0, 0, (int)newImageWidth, (int)newImageHeight);
    thumbnailGraph.DrawImage((Bitmap)originalImage, imageRectangle);                
    
    
                    

    I am sorry but I don't see how that is saving the image as an Image object.

    I am using GD+ to create a thumbnail so I use something like what I pasted above. The problem is that I want to do some more stuff using the Image object.

    Can I just cast the bitmap to the Image type?


    var thumbnailBitmap = new Bitmap((int)newImageWidth, (int)newImageHeight);
                    var thumbnailGraph = Graphics.FromImage(thumbnailBitmap);



    Tuesday, July 6, 2010 2:50 PM
  • User-1397778012 posted

    Doh, it looks like you don't even need to cast.

    I just did Image originalImage = thumbnailBitmap;

    Tuesday, July 6, 2010 3:14 PM
  • User-474337052 posted
    Image originalImage = Image.FromFile(fromPath);
            var thumbnailBitmap = new Bitmap(originalImage,(int)newImageWidth, (int)newImageHeight);
            using (Graphics g = Graphics.FromImage(thumbnailBitmap))
            {
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                g.DrawImage(thumbnailBitmap, 0, 0, (int)newImageWidth, (int)newImageHeight);
                g.Dispose(); originalImage.Dispose(); GC.Collect();
            }
    
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 6, 2010 3:20 PM