C # Why does Bitmap.Save ignore PixelFormat Bitmap?

I need to process and save the images in the original bpp (1 for BnW, 8 for gray, 24 for color). After processing, I always have 24bpp (due to processing with Aforge.Net). I pass the original bpp function to save, and I use the following code to save:

private static Bitmap changePixelFormat(Bitmap input, PixelFormat format)
    {
        Bitmap retval = new Bitmap(input.Width, input.Height, format);
        retval.SetResolution(input.HorizontalResolution, input.VerticalResolution);
        Graphics g = Graphics.FromImage(retval);
        g.DrawImage(input, 0, 0);
        g.Dispose();
        return retval;
    }

When I pass:

PixelFormat.Format8bppIndexed

I get an exception: "A graphic cannot create an image in indexed pixel format" on the line:

Graphics g = Graphics.FromImage(retval);

I tried the following code:

Bitmap s = new Bitmap("gray.jpg");
        Bitmap NewPicture = s.Clone(new Rectangle(0, 0, s.Width, s.Height),                   PixelFormat.Format8bppIndexed);

After that, "NewImage" has a PixelFormat 8bppIndexed, but when I save NewImage:

NewPicture.Save("hello.jpg", ImageFormat.Jpeg);

hello.jpg has 24bpp.

I need to save bpp format and image source image.

Why does Bitmap.Save ignore PixelFormat Bitmap?

==================================================== =====

, , : http://freeimage.sourceforge.net/license.html

( JPEG) 8- .

+3
4

, 1bpp/8bpp. 1bpp/8bpp #.

changePixelFormat , , , 1bpp 8 bpp, , .

Graphics.FromImage :

, "A ". .

Format1bppIndexed

Format4bppIndexed

Format8bppIndexed

:

, :

8bpp 8bpp jpg gdiplus 1,0. Windows 7 gdiplus, .

8bpp jpeg- Microsoft WIC API.

, WIC- Microsoft .

/ GDI + 8

JPEG 24- .

+4

, JPEG . , PixelFormat.Format8bppIndexed, GDI + , JPEG. , 24 bpp.

BMP GIF. :

NewPicture.Save("hello.jpg", ImageFormat.Bmp);

. , , .

+5

8- , fooobar.com/questions/115844/... . (Windows/Mac/Linux), Windows 7. , .

0

Use the Graphics class for a non-indexed image. UseBitmap.LockBits and Bitmap.UnlockBits when processing indexed images

0
source

Source: https://habr.com/ru/post/1785184/


All Articles