locked
Saving to file as .PNG RRS feed

  • Question

  • User285621953 posted
    I have been saving images to a file like this:MyBMP.Save(Request.MapPath("/portalvb/images/thumbs/") & newname &".jpg",ImageFormat.Jpeg)and would like to save them to the file as PNG format. I found examples that show to use dim memStream As New MemoryStream() MyBMP.Save(memStream,ImageFormat.Png) memStream.WriteTo(response.outputstream) My problem is not really the .png format, but using response.outputstring instead of the destination that I want the image to be saved in.
    Wednesday, March 31, 2004 10:13 AM

All replies

  • User-599719271 posted
    what's wrong with this : MyBMP.Save(Request.MapPath("/portalvb/images/thumbs/") & newname &".png",ImageFormat.Png)
    Wednesday, March 31, 2004 5:47 PM
  • User285621953 posted
    I tried it that way and it doesn't work because Png format requires this extra step. bmp.Save(memStream, ImageFormat.Png) memStream.WriteTo(Response.OutputStream) This article explains "An error is returned as some image formats, including .png, can only be saved to 'seekable' streams and this excludes Response.OutputStream. 'Seeking' is the ability to examine any of the items in the stream. The solution is to use an intermediary stream that does support this ability, in this case a MemoryStream". Thanks for the reply
    Thursday, April 1, 2004 8:58 AM
  • User-599719271 posted
    Ah. I see. I'll do some checking round. bear with me.
    Friday, April 2, 2004 12:42 AM
  • User-1372641848 posted
    Hi There should not be any problem while saving Png format on filesystem. MyBMP.Save(Request.MapPath("/portalvb/images/thumbs/") & newname ".jpg",ImageFormat.Jpeg) Should work correctly. But when you try to save image to Png format to stream which is non seeakable it will throw error. and only for such case you will need have to save image first to memorystream. See following code. <%@ Page Language="C#" Debug="true" EnableViewState="false" %> <%@ import Namespace="System.IO" %> <%@ import Namespace="System.Drawing" %> <%@ import Namespace="System.Drawing.Imaging" %> <script runat="server"> void Page_Init(Object sender, EventArgs e){ System.Drawing.Bitmap img = new Bitmap(200,100); Graphics g = Graphics.FromImage(img); g.Clear(Color.White); g.DrawString("Png Image",new Font("Verdana",10),Brushes.Black,10,10); // It does not have any problem with this img.Save("c:\\test\\png.png",ImageFormat.Png); // This Won't Work because it needs seekable stream. // img.Save(Response.OutputStream,ImageFormat.Png); // This will work as we are saving image first in seekable // MemoryStream and then written to OutputStream MemoryStream memStream = new MemoryStream(); img.Save(memStream,ImageFormat.Png); memStream.WriteTo(Response.OutputStream); g.Dispose(); img.Dispose(); } </script>
    Saturday, April 3, 2004 11:09 AM
  • User285621953 posted
    Thanks fro the response. This does save the file as a .PNG, but it makes it an image with a filesize 10x larger than the original. It turns a 50k jpg into a 500k png and a 5k gif into a 50k png. Any idea why? Dim SourceBitmap As Bitmap = New Bitmap(Server.MapPath(savePath & "original." & c)) Dim TargetBitmap As Bitmap = New Bitmap(NewWidth,NewHeight) Dim g As Graphics = Graphics.FromImage(TargetBitmap) ' set Drawing Quality g.InterpolationMode = InterpolationMode.HighQualityBicubic g.SmoothingMode = SmoothingMode.HighQuality g.Clear(Color.White) Dim compressionRectangle As Rectangle = New Rectangle(0,0,NewWidth,NewHeight) g.DrawImage(SourceBitmap, compressionRectangle) g.DrawString("TEST PNG",New Font("Verdana Bold", 10),New SolidBrush(Color.SteelBlue),10,10) Response.ContentType = "image/png" ' decide new path and save the image Dim NewImagePath As String = System.IO.Path.GetFileNameWithoutExtension(OriginalImageName) + ".png" 'dim memStream As New MemoryStream((Request.MapPath("/images/TESTPNG/") &"LARGE.png")) TargetBitmap.Save((Request.MapPath("\images\TESTPNG\") &"LRG.png"),ImageFormat.Png) 'TargetBitmap.Save(memStream,ImageFormat.Png) 'memStream.WriteTo(response.outputstream) 'tidy up TargetBitmap.dispose()Thanks
    Saturday, April 3, 2004 6:31 PM