Solved

JPEG Compression Issue

Posted on 2008-10-10
4
933 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now