Showing posts with label images. Show all posts
Showing posts with label images. Show all posts

Saturday, March 31, 2012

System.Drawing.Image.Save() on Transparent GIF

I am creating GIF images with transparent backgrounds on-the-fly for a web
app and rendering them by using
System.Drawing.Image.Save(Response.OutputStream, ImageType.GIF).

I am confident that the transparency is working properly because if I save
the created image to the local hard disk and then view it in a web page or an
image editor, the transparency is correct. I can also view the transparency
on-the-fly in a Windows.Forms application.

Yet, when I render the image on-the-fly to the Response.OutputStream, I lose
the transparency and the background color is displayed in the page.

Has anyone got any ideas or experience in rendering transparent GIFs this way?

Thanks,
--
Dale Preston
MCAD C#
MCSE, MCDBAHello Dale,

From your description, you've programmatically create an transparent gif
image and flush it into ASP.NET page's response stream. However, you found
the output image lose the transparency, correct?

Based on my understanding, it is likely that the bits save into the
response stream is different from the expected transparent gif's binary
bits. I suggest you test through the following means:

1. first use your code to generate the gif and save it into a disk file(gif
file), make sure that the gif file is working as transparent one.

2. use ASP.NET code to write the gif file into response stream as below:

===============
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Response.ClearHeaders()
Response.ClearContent()
Response.ContentType = "image/gif"

Response.WriteFile("D:\temp\web_temp\images\duglogobigtextcolor.gif")

Response.End()

End Sub
===============

based on my test, the above code can correctly flush a correct transparent
gif out to client-side. If the above test also return a non-transparent
one, it seems the generated gif has some bits get corrupted.

Please feel free to let me know if you have any other finding or anything I
missed.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

This posting is provided "AS IS" with no warranties, and confers no rights.
Thanks, Steven. I was afraid you were going to say that. Our web
infrastructure policies don't generally allow web apps to write to the local
disk so that's going to be a problem. I may have to look for another
alternative.

The goal was to present an error message in a text bubble with the bubble
hook pointing to the form field with the improper value. The transparent GIF
seemed like just the ticket.

Do you have any other suggestions or know of articles or examples of
alternative methods for displaying text in a bubble like that on-the-fly?

I built my code following the examples in KB #319061 and
http://www.bobpowell.net/giftransparency.htm. Do you know if I would have
the same problem with a transparent PNG? If a PNG is a possibility, do you
know of any documentation on how to set the transparency in a PNG using .Net?
--
Dale Preston
MCAD C#
MCSE, MCDBA

"Steven Cheng[MSFT]" wrote:

Quote:

Originally Posted by

Hello Dale,
>
From your description, you've programmatically create an transparent gif
image and flush it into ASP.NET page's response stream. However, you found
the output image lose the transparency, correct?
>
Based on my understanding, it is likely that the bits save into the
response stream is different from the expected transparent gif's binary
bits. I suggest you test through the following means:
>
1. first use your code to generate the gif and save it into a disk file(gif
file), make sure that the gif file is working as transparent one.
>
2. use ASP.NET code to write the gif file into response stream as below:
>
===============
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Response.ClearHeaders()
Response.ClearContent()
Response.ContentType = "image/gif"
>
>
Response.WriteFile("D:\temp\web_temp\images\duglogobigtextcolor.gif")
>
Response.End()
>
>
>
End Sub
===============
>
based on my test, the above code can correctly flush a correct transparent
gif out to client-side. If the above test also return a non-transparent
one, it seems the generated gif has some bits get corrupted.
>
Please feel free to let me know if you have any other finding or anything I
missed.
>
Sincerely,
>
Steven Cheng
>
Microsoft MSDN Online Support Lead
>
>
This posting is provided "AS IS" with no warranties, and confers no rights.
>
>


Just FYI, Steven.

I was able to resolve my problem and, hopefully make my solution more robust
than it may have otherwise been.

The problem was that the Bitmap object I was drawing on using the Graphics
class seems to corrupt the palette by duplicating a single palette entry
which was, coincidentally, the transparent color.

While I was aware of the duplication, I was skipping the first occurrence
because no pixels pointed to it. The second occurrence was where all the
transparent pixels pointed and that is the entry I set the alpha to 0 on.
This issue did not cause any problems in Windows.Forms, in Paint Shop Pro, in
Microsoft Image Composer, or if I saved to disk and then opened the saved
file in a web page.

For some reason, though, it did create a problem when saving to the
Response.OutputStream because the method requires re-assigning the ImageType.
There should be an overload of the Save method that writes to a Stream
without having to pass the ImageType just as there is an overload that saves
to a file.

The bottom line is, I re-wrote my code to detect duplicate entries in the
palette, then modify all pixels that pointed to any of the duplicates so that
they point to the first occurrence of the color, and then modify all of the
remaining duplicate palette entries so that they are unique. With this done,
I was able to save my image to the Response.OutputStream with the
transparency working as expected.

Thanks again for your help.

Dale
--
Dale Preston
MCAD C#
MCSE, MCDBA

"Dale" wrote:

Quote:

Originally Posted by

Thanks, Steven. I was afraid you were going to say that. Our web
infrastructure policies don't generally allow web apps to write to the local
disk so that's going to be a problem. I may have to look for another
alternative.
>
The goal was to present an error message in a text bubble with the bubble
hook pointing to the form field with the improper value. The transparent GIF
seemed like just the ticket.
>
Do you have any other suggestions or know of articles or examples of
alternative methods for displaying text in a bubble like that on-the-fly?
>
I built my code following the examples in KB #319061 and
http://www.bobpowell.net/giftransparency.htm. Do you know if I would have
the same problem with a transparent PNG? If a PNG is a possibility, do you
know of any documentation on how to set the transparency in a PNG using .Net?
--
Dale Preston
MCAD C#
MCSE, MCDBA
>
>
"Steven Cheng[MSFT]" wrote:
>

Quote:

Originally Posted by

Hello Dale,

From your description, you've programmatically create an transparent gif
image and flush it into ASP.NET page's response stream. However, you found
the output image lose the transparency, correct?

Based on my understanding, it is likely that the bits save into the
response stream is different from the expected transparent gif's binary
bits. I suggest you test through the following means:

1. first use your code to generate the gif and save it into a disk file(gif
file), make sure that the gif file is working as transparent one.

2. use ASP.NET code to write the gif file into response stream as below:

===============
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Response.ClearHeaders()
Response.ClearContent()
Response.ContentType = "image/gif"

Response.WriteFile("D:\temp\web_temp\images\duglogobigtextcolor.gif")

Response.End()

End Sub
===============

based on my test, the above code can correctly flush a correct transparent
gif out to client-side. If the above test also return a non-transparent
one, it seems the generated gif has some bits get corrupted.

Please feel free to let me know if you have any other finding or anything I
missed.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

This posting is provided "AS IS" with no warranties, and confers no rights.


Hi Dale,

Thanks for your followup.

I'm glad that you've figured out the problem and found a workable solution.

As always, welcome to post here when you need any help from us.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

This posting is provided "AS IS" with no warranties, and confers no rights.

System.Drawing.Image.Save() on Transparent GIF

I am creating GIF images with transparent backgrounds on-the-fly for a web
app and rendering them by using
System.Drawing.Image.Save(Response.OutputStream, ImageType.GIF).
I am confident that the transparency is working properly because if I save
the created image to the local hard disk and then view it in a web page or a
n
image editor, the transparency is correct. I can also view the transparency
on-the-fly in a Windows.Forms application.
Yet, when I render the image on-the-fly to the Response.OutputStream, I lose
the transparency and the background color is displayed in the page.
Has anyone got any ideas or experience in rendering transparent GIFs this wa
y?
Thanks,
--
Dale Preston
MCAD C#
MCSE, MCDBAHello Dale,
From your description, you've programmatically create an transparent gif
image and flush it into ASP.NET page's response stream. However, you found
the output image lose the transparency, correct?
Based on my understanding, it is likely that the bits save into the
response stream is different from the expected transparent gif's binary
bits. I suggest you test through the following means:
1. first use your code to generate the gif and save it into a disk file(gif
file), make sure that the gif file is working as transparent one.
2. use ASP.NET code to write the gif file into response stream as below:
===============
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Response.ClearHeaders()
Response.ClearContent()
Response.ContentType = "image/gif"
Response.WriteFile(" D:\temp\web_temp\images\duglogobigtextco
lor.gif")
Response.End()
End Sub
===============
based on my test, the above code can correctly flush a correct transparent
gif out to client-side. If the above test also return a non-transparent
one, it seems the generated gif has some bits get corrupted.
Please feel free to let me know if you have any other finding or anything I
missed.
Sincerely,
Steven Cheng
Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.
Thanks, Steven. I was afraid you were going to say that. Our web
infrastructure policies don't generally allow web apps to write to the local
disk so that's going to be a problem. I may have to look for another
alternative.
The goal was to present an error message in a text bubble with the bubble
hook pointing to the form field with the improper value. The transparent GI
F
seemed like just the ticket.
Do you have any other suggestions or know of articles or examples of
alternative methods for displaying text in a bubble like that on-the-fly?
I built my code following the examples in KB #319061 and
http://www.bobpowell.net/giftransparency.htm. Do you know if I would have
the same problem with a transparent PNG? If a PNG is a possibility, do you
know of any documentation on how to set the transparency in a PNG using .Net
?
--
Dale Preston
MCAD C#
MCSE, MCDBA
"Steven Cheng[MSFT]" wrote:

> Hello Dale,
> From your description, you've programmatically create an transparent gif
> image and flush it into ASP.NET page's response stream. However, you found
> the output image lose the transparency, correct?
> Based on my understanding, it is likely that the bits save into the
> response stream is different from the expected transparent gif's binary
> bits. I suggest you test through the following means:
> 1. first use your code to generate the gif and save it into a disk file(gi
f
> file), make sure that the gif file is working as transparent one.
> 2. use ASP.NET code to write the gif file into response stream as below:
> ===============
> Protected Sub Page_Load(ByVal sender As Object, ByVal e As
> System.EventArgs) Handles Me.Load
> Response.ClearHeaders()
> Response.ClearContent()
> Response.ContentType = "image/gif"
>
> Response.WriteFile(" D:\temp\web_temp\images\duglogobigtextco
lor.gif")
> Response.End()
>
> End Sub
> ===============
> based on my test, the above code can correctly flush a correct transparent
> gif out to client-side. If the above test also return a non-transparent
> one, it seems the generated gif has some bits get corrupted.
> Please feel free to let me know if you have any other finding or anything
I
> missed.
> Sincerely,
> Steven Cheng
> Microsoft MSDN Online Support Lead
>
> This posting is provided "AS IS" with no warranties, and confers no rights
.
>
Just FYI, Steven.
I was able to resolve my problem and, hopefully make my solution more robust
than it may have otherwise been.
The problem was that the Bitmap object I was drawing on using the Graphics
class seems to corrupt the palette by duplicating a single palette entry
which was, coincidentally, the transparent color.
While I was aware of the duplication, I was skipping the first occurrence
because no pixels pointed to it. The second occurrence was where all the
transparent pixels pointed and that is the entry I set the alpha to 0 on.
This issue did not cause any problems in Windows.Forms, in Paint Shop Pro, i
n
Microsoft Image Composer, or if I saved to disk and then opened the saved
file in a web page.
For some reason, though, it did create a problem when saving to the
Response.OutputStream because the method requires re-assigning the ImageType
.
There should be an overload of the Save method that writes to a Stream
without having to pass the ImageType just as there is an overload that saves
to a file.
The bottom line is, I re-wrote my code to detect duplicate entries in the
palette, then modify all pixels that pointed to any of the duplicates so tha
t
they point to the first occurrence of the color, and then modify all of the
remaining duplicate palette entries so that they are unique. With this done
,
I was able to save my image to the Response.OutputStream with the
transparency working as expected.
Thanks again for your help.
Dale
--
Dale Preston
MCAD C#
MCSE, MCDBA
"Dale" wrote:
> Thanks, Steven. I was afraid you were going to say that. Our web
> infrastructure policies don't generally allow web apps to write to the loc
al
> disk so that's going to be a problem. I may have to look for another
> alternative.
> The goal was to present an error message in a text bubble with the bubble
> hook pointing to the form field with the improper value. The transparent
GIF
> seemed like just the ticket.
> Do you have any other suggestions or know of articles or examples of
> alternative methods for displaying text in a bubble like that on-the-fly?
> I built my code following the examples in KB #319061 and
> http://www.bobpowell.net/giftransparency.htm. Do you know if I would have
> the same problem with a transparent PNG? If a PNG is a possibility, do yo
u
> know of any documentation on how to set the transparency in a PNG using .N
et?
> --
> Dale Preston
> MCAD C#
> MCSE, MCDBA
>
> "Steven Cheng[MSFT]" wrote:
>
Hi Dale,
Thanks for your followup.
I'm glad that you've figured out the problem and found a workable solution.
As always, welcome to post here when you need any help from us.
Sincerely,
Steven Cheng
Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.

Thursday, March 22, 2012

System.OutOfMemoryException: Out of memory.

Hello all

I have a ASPX file called scaleimage.aspx that scales my images down
to a given width. This page is used very much in web project I am
working on.

But due to the large size of the images I randomly get [X] images
(error) in Internet Explorer. When I open one individual image at that
point I read this exception:

[OutOfMemoryException: Out of memory.]
System.Drawing.Bitmap..ctor(String filename) +197
tekno.scaleimage.Page_Load(Object sender, EventArgs e)
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +731

Any suggestions are more than welcome. I hope you can help me.
Thank you in advance.

Regards,

Freek Versteijn

See here my code:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Request("path") Is Nothing Or Request("width") Is Nothing
Then
Return
End If

Dim img As Image = New
Bitmap(Server.MapPath(Server.UrlDecode(Request("path"))))
img = resizeImage(img, Request("width"), 0)

Response.ClearHeaders()
Response.ClearContent()
Response.Clear()
Response.ContentType = "Image/JPEG"

img.Save(Response.OutputStream, Imaging.ImageFormat.Jpeg)
Response.End()

'Dispose the image object immediately to limit memory use in
case highres/large images are used
img.Dispose()
End Sub

Public Function ResizeImage(ByRef image As Image, ByVal maxWidth
As Integer, ByVal maxHeight As Integer)
Dim imgHeight, imgWidth As Double

Dim bm As Bitmap = New Bitmap(image)

imgHeight = bm.Height
imgWidth = bm.Width

If (maxWidth > 0 And imgWidth > maxWidth) Or (maxHeight > 0
And imgHeight > maxHeight) Then
'Determine what dimension is off by more
Dim deltaWidth As Double = imgWidth - maxWidth
Dim deltaHeight As Double = imgHeight - maxHeight
Dim scaleFactor As Double

'If (deltaHeight > deltaWidth) Then
'Scale by the height
'scaleFactor = maxHeight / imgHeight
'Else
' 'Scale by the Width
scaleFactor = maxWidth / imgWidth
'End If

imgWidth *= scaleFactor
imgHeight *= scaleFactor
End If

Dim w As Integer = Convert.ToInt32(imgWidth)
Dim h As Integer = Convert.ToInt32(imgHeight)
Dim inp As IntPtr = New IntPtr
Dim bmp As System.Drawing.Image = bm.GetThumbnailImage(w, h,
Nothing, inp)

Return bmp
End FunctionHi, Versteijn,

Did you try using the DrawImage method on the Graphics class instead? You
will have to change the following lines:

> Dim inp As IntPtr = New IntPtr
> Dim bmp As System.Drawing.Image = bm.GetThumbnailImage(w, h, Nothing, inp)

to:

Dim bmp As New Bitmap(w, h)
Graphics.FromImage(bmp).DrawImage(bm, w, h)

Hope this helps
Martin
Sorry, it should be another overload:

Graphics.FromImage(bmp).DrawImage(bm, 0, 0, w, h)

It is so easy with the IntelliSence...

Greetings
Martin
"Martin Dechev" <detcheff_@.hotmail.com> wrote in message news:<#uSXmc3$DHA.2660@.TK2MSFTNGP10.phx.gbl>...
> Sorry, it should be another overload:
> Graphics.FromImage(bmp).DrawImage(bm, 0, 0, w, h)
> It is so easy with the IntelliSence...
> Greetings
> Martin

Hi Martin,

Thanks, I'll try it! Why do you think this will spare memory?

Thank you.

Freek Versteijn
Hi, Freek Versteijn,

I don't know from which call the OutOfMemory originates, but I tested with a
2 MB bitmap scaling it to ~75% and it was working with the both methods. As
it is stated in the documentation for the GetThumbnailImage method it gives
good quality for 120x120, but lacks in quality for higher resolutions.

http://msdn.microsoft.com/library/e...mageTopic .asp

Greetings
Martin
"Versteijn" <versteijn@.538mail.nl> wrote in message
news:4d19834f.0403010822.54274e09@.posting.google.c om...
> "Martin Dechev" <detcheff_@.hotmail.com> wrote in message
news:<#uSXmc3$DHA.2660@.TK2MSFTNGP10.phx.gbl>...
> > Sorry, it should be another overload:
> > Graphics.FromImage(bmp).DrawImage(bm, 0, 0, w, h)
> > It is so easy with the IntelliSence...
> > Greetings
> > Martin
> Hi Martin,
> Thanks, I'll try it! Why do you think this will spare memory?
> Thank you.
> Freek Versteijn
> > Hi Martin,
> > Thanks, I'll try it! Why do you think this will spare memory?
> > Thank you.
> > Freek Versteijn

Hi Martin,

Changed my code a bit (see under), but ever call to the page uses
about 10-50 mb RAM, which is not freed after the response has been
send. After a while the process can't address any more memory.

The cause of the high memory use is the GetThumbnailImage function.

Regards,

Freek Versteijn

Here is my current code:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Request("path") Is Nothing Or Request("width") Is Nothing
Then
Return
End If

Dim bmp As Bitmap = New
Bitmap(Server.MapPath(Server.UrlDecode(Request("path"))))
ResizeImage(bmp, Request("width"), 0)

Response.ClearHeaders()
Response.ClearContent()
Response.Clear()
Response.ContentType = "Image/JPEG"

bmp.Save(Response.OutputStream, Imaging.ImageFormat.Jpeg)
Response.End()

'Dispose the image object immediately to limit memory use in
case highres/large images are used
bmp.Dispose()
End Sub

Public Sub ResizeImage(ByRef bmp As Bitmap, ByVal maxWidth As
Integer, ByVal maxHeight As Integer)
Dim imgHeight, imgWidth As Double
Dim bm As Bitmap = New Bitmap(bmp)

imgHeight = bm.Height
imgWidth = bm.Width

If (maxWidth > 0 And imgWidth > maxWidth) Or (maxHeight > 0
And imgHeight > maxHeight) Then
'Determine what dimension is off by more
Dim deltaWidth As Double = imgWidth - maxWidth
Dim deltaHeight As Double = imgHeight - maxHeight
Dim scaleFactor As Double

'If (deltaHeight > deltaWidth) Then
'Scale by the height
'scaleFactor = maxHeight / imgHeight
'Else
' 'Scale by the Width
scaleFactor = maxWidth / imgWidth
'End If

imgWidth *= scaleFactor
imgHeight *= scaleFactor
End If

Dim w As Integer = Convert.ToInt32(imgWidth)
Dim h As Integer = Convert.ToInt32(imgHeight)
Dim inp As IntPtr = New IntPtr
bmp = bm.GetThumbnailImage(w, h, Nothing, inp)
End Sub