Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: GDI GDI+ C#4.0
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 bitmapConfused | :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? Big Grin | :-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 | :sigh:
 
thanks for the response(s) if any! Smile | :)
Posted 7-Jul-10 11:38am
wpte1.2K

1 solution

Rate this: bad
good
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 ?
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 565
1 Maciej Los 359
2 Kornfeld Eliyahu Peter 309
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,303
1 DamithSL 4,764
2 Maciej Los 4,306
3 Kornfeld Eliyahu Peter 3,914
4 Sergey Alexandrovich Kryukov 3,538


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 7 Jul 2010
Copyright © CodeProject, 1999-2014
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