Click here to Skip to main content
13,006,180 members (66,689 online)
Rate this:
Please Sign up or sign in to vote.
See more:
The code I have so far:
public static void SaveJpeg(string path, Image img)
            // Jpeg image codec 
            ImageCodecInfo jpegCodec = GetEncoderInfo("image/jpeg");
            Bitmap bmp = new Bitmap(img);
            Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
            System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.WriteOnly, bmp.PixelFormat);
            EncoderParameters encoderParams = bmp.GetEncoderParameterList(jpegCodec.Clsid);
            img.Save(path, jpegCodec, encoderParams);

What I'm trying to do is to get the EncoderParameters from a JPEG and save that to another JPEG.

I've tried doing this the regular way by doing img.GetEncoderParameterList(jpegCodec.Clsid) which should have worked fine, but I got an error: "Bitmap region is already locked." where I create the object EncoderParameters encoderParams (and assign it).

So I searched here and there, and found that I had to lock and unlock the bits, which is only possible for a bitmap:confused:
Now in the example above I've done it in the bitmap way, but it still comes up with the same error, even with different imagelockmodes.

All I want is to save the JPEG, and with the bmp as encoder, the parameters are probably different (I guess).
Is there a way? :-D

I'm pretty new to .net and imaging, but this is the first problem I've had that I can't solve myself :sigh:

thanks for the response(s) if any! :)
Posted 7-Jul-10 10:38am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

What makes you think you can 'only lock bits for a bitmap' ? Once it's loaded, it IS A bitmap, you think GDI+ keeps a jpeg in memory ? You're creating a bitmap from an image in memory. Are you sure that it contains JPEG data ? Why not load that info when you load the image and store it in memory ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170628.1 | Last Updated 7 Jul 2010
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100