Solved

JPEG Compression Issue

Posted on 2008-10-10
4
936 Views
Last Modified: 2013-11-08
Hello, I am changing the default compression set on an jpeg using the following code:

                    'Setup a JPEG codec with desired quality
                    Dim Codec, JPEGCodec As System.Drawing.Imaging.ImageCodecInfo
                    Dim EncParams As System.Drawing.Imaging.EncoderParameters

                    For Each Codec In System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders
                        If Codec.MimeType = "image/jpeg" Then
                            JPEGCodec = Codec
                            Exit For
                        End If
                    Next 'Supposedly this cannot fail as image/jpg is built-in to GDI+ encoders

                    EncParams = New System.Drawing.Imaging.EncoderParameters(1)
                    EncParams.Param(0) = New System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, imageCompression)

                    HttpContext.Current.Response.ContentType = "image/jpeg"
                    outputImage.Save(HttpContext.Current.Response.OutputStream, JPEGCodec, EncParams)
                    'outputImage.Save(HttpContext.Current.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)

It works great but not in IE6.  The browser just crashes.  When I uncomment the last line an comment out the line before the last it work fine.  Is there a way to fix this or is there another way to change the default compression applied to an image when it is saved?

Thanks
0
Comment
Question by:grogo21
  • 2
  • 2
4 Comments
 
LVL 11

Accepted Solution

by:
saleek earned 500 total points
ID: 22685054
hi,

what are you setting compression quality to?

For example, is it something like:

Dim imageCompression as Long = 99L  <-- 99 is quality percentage you would like

I dont see why IE6 would crash, if the aspx page outputs a valid bitmap then there should be no issue.

Does it work in IE7?

regards,

saleek
0
 

Author Comment

by:grogo21
ID: 22688602
Hello,
Yes I am using something like Dim imageCompression as Long = 99L.

I don't see why it would crash either. It works on IE7 and all other browsers.  For now I am using the following to get around it:

                    If HttpContext.Current.Request.UserAgent.Contains("MSIE 6") Then
                        outputImage.Save(HttpContext.Current.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)
                    Else
                    outputImage.Save(HttpContext.Current.Response.OutputStream, JPEGCodec, EncParams)
                    End If

I did use fiddler and the responses were identical with and without compression.  IE6 just crashes for some reason when I use a custom compression.  Is this something I can fix.  It seems to be an issue with the browser.

THanks
0
 

Author Comment

by:grogo21
ID: 22688872
Actually If I comment out this line flater in my code I can use compression just fine in IE6:

            'HttpContext.Current.Response.Close()

Any ideas why?

Thanks
0
 
LVL 11

Expert Comment

by:saleek
ID: 22794111
No - but I I have no idea why you would close the response.

Perhaps IE could not load the image (compress it?) beore you closed the response.

All the best.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question