Showing posts with label webapp. Show all posts
Showing posts with label webapp. 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

What should I do to combat this error? I am doing fairly heavy dataset caching on my webapp. I have 2 gigs memory, and aspwp.exe is using up to 1.3gigs. This caching is essential to my application as it saves ALOT of database queries.

I thought asp.net would recycle what it needed to. Is the best solution to just thro more RAM at it? Thats what I plan on doing, but its going to take a few days to get the server up and running and need a solution in the meantime. Also I am worried what is going to happen when I have my server maxed out on ram usage and I get this error

thanks for any help

mike123Are you setting any priority on the cache items when adding them? Reason I ask is that if everything is being set to "high", it may not be recycling anything w/ the garbage collection.

Also, when you add things to cache, are you checking to see if they already exist or possibly even deleting the old cache before adding it again?
Are any of your caches set to time out (I would think so).

If your cache is too broad, I can see this being a problem. If you are caching a search page, it would be very possible for you to hit the limit of your RAM.

Charley.
I am not setting priority on anything, not high or low. Here is an example of how I cache my dataset. I'm not sure about deleting the old cache before adding it again, because I dont think that happens since I check before I add, please correct me if im wrong on that.

Also, Charley I believe you may be exactly correct on the searches taking all of my memory, I am looking at making them expire quicker and setting their priority low. Is this what you would recommend? How would you modify this function?

Thanks alot
mike123

Public Function QuestionsGet(ByVal userID As Integer) As DataSet

Dim myDataSet As New DataSet()

If HttpContext.Current.Cache.Get("select_questions" & CStr(userID)) Is Nothing Then

' Create Instance of Connection and Command Object
Dim objConn As New SqlConnection("uid=")
Dim objCommand As New SqlDataAdapter("select_questions", objConn)

' Mark the Command as a SPROC
objCommand.SelectCommand.CommandType = CommandType.StoredProcedure

' Add Parameters to SPROC
Dim parameterUserID As New SqlParameter("@.userID", SqlDbType.Int, 4)
parameterUserID.Value = userID
objCommand.SelectCommand.Parameters.Add(parameterUserID)

'Fill the DataSet
objCommand.Fill(myDataSet, "select_questions")
HttpContext.Current.Cache.Item("select_questions" & CStr(userID)) = myDataSet

Else
myDataSet = CType(HttpContext.Current.Cache.Get("select_questions" & CStr(userID)), DataSet)
End If

Return myDataSet

End Function
Tell us a bit about what you are trying to accomplish.

What type of data are you returning?

I see 'questions' based by the user Id.

Are these very static?

If so, a different way to approch this might be to generate an XML file durning a nightly batch, and instead of reading the DB, read that file instead...
They are fairly static I have probably 20k cached, but not static enough to do a nightly update I believe. I have at least a few hundred changing daily. They are directly tied to users info, I have cached each users info. The data is about 8 text strings shorter than 500 chars.

Thanks for the suggestion, but I dont think it will work in this case. What do you think?

Also, I am attempting to add ", Caching.CacheItemPriority.Low" to my cache statement, but its not accepting the final argument. Am I approaching this wrong?

Thanks again
mike123
anyone? I'm still getting this error and have no idea what to do! :(

thanks again
mike123
Mike.

I'm just throwing out ideas here...

Instead of caching the datasets, have you tried caching the page itself?

Some other options are (Not saying these are the BEST options, but options nonetheless):
- Disable caching until you get more RAM
- Reevaluate queries and see if you can combine anything
- Reevaluate your architecture so that you aren't as database intensive on any given page

Are your pages hitting directly against the database?
Charley,

Unforunately I can't output cache the page, because I need to manually expire it when certain occurances happen. One page in particular has 4 queries, and is being hit about 1 million times per day, so if I lose the caching my database server is going to have soem seroius extra load on it. I would love to get more RAM, however I need a scalable solution and I dont want the same thing to happen when I max out on my new server with 4gigs instead of 2.

Thanks for the input

mike123
4 million queries a day... yowsers...

What the heck kind of app is this?

Have you done performance monitoring on the SQL server?

If you have caching off, does it peg that server?
It's a web app where users have their own personal page, that is database driven. Yes, it sure does peg the server if I turn caching off so I really appreciate the caching. It just really sucks to have it crap out when it runs out of memory, I thought asp.net would be a little better at handling it.

I can upgrade to 4 gigs, but thats just a temporary solution and its goign to cost alot of time / money.

Thanks