Click here to Skip to main content
14,738,282 members
Please Sign up or sign in to vote.
4.00/5 (1 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);
            bmp.UnlockBits(bmpData);

            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

1 solution

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)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900