GDI + Download jpg and save as 24-bit png problem

Problem

Hello everybody!

I have this code that processes my jpg images by changing the pixels and finally saves them as a png type. The problem is that the resulting image has a bit depth of 32 bits. I need it to be 24 bits, can someone light some kind of light on the correct way to set it up? I'm looking at the right roads, how to set the pixel format on PixelFormat24bppRGB?

the code

static inline void Brighten(Gdiplus::Bitmap* img)
{
    int width = img->GetWidth()/8,height = img->GetHeight(), max = (width*height),r,g,b;
    Gdiplus::Color pixel;   
    for(int a = 0,x = 0, y = -1; a < max; ++a)
    {
        x = a%width;    
        if(x == 0)
            ++y;
        img->GetPixel(x,y,&pixel);
        r = pixel.GetR();
        g = pixel.GetG();
        b = pixel.GetB();
        if (r > 245) r = 245; 
        if (g > 245) g = 245;  
        if (b > 245) b = 245; 
        r += 10;
        g += 10;
        b += 10;
        pixel = Gdiplus::Color(r,g,b);
        img->SetPixel(x,y,pixel);;
    }
}

ULONG_PTR m_dwToken = 0;
Gdiplus::GdiplusStartupInput input;
Gdiplus::GdiplusStartupOutput output;
Gdiplus::GdiplusStartup( &m_dwToken, &input, &output );
USES_CONVERSION_EX;
Gdiplus::ImageCodecInfo* pEncoders = static_cast< Gdiplus::ImageCodecInfo* >( _ATL_SAFE_ALLOCA(1040, _ATL_SAFE_ALLOCA_DEF_THRESHOLD));
Gdiplus::DllExports::GdipGetImageEncoders(5, 1040, pEncoders );
CLSID clsidEncoder = pEncoders[4].Clsid;

Gdiplus::Bitmap img1((CT2W)L"IMG_1.JPG");
Brighten(&img1);
img1.Save((CT2W)L"IMG_1_R3.PNG",&clsidEncoder,NULL);

Thanks in advance!

+3
source share
1 answer

, , LockBits() , 24- .

static inline void Brighten(Gdiplus::Bitmap* img)
{
    int width = img->GetWidth(),height = img->GetHeight(),r,g,b;
    Gdiplus::Rect rect(0,0,width,height);
    Gdiplus::BitmapData data;
    img->LockBits(&rect,Gdiplus::ImageLockModeWrite,PixelFormat24bppRGB,&data);
    int stride = data.Stride,offset = stride - width*3;
    byte * p1 = (byte *)(void *)data.Scan0;
    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            r = p1[0];
            g = p1[1];
            b = p1[2];
            if (r > 245) r = 245; 
            if (g > 245) g = 245;  
            if (b > 245) b = 245; 
            r += 10;
            g += 10;
            b += 10;    
            p1[0] = r;
            p1[1] = g;
            p1[2] = b;
            p1 += 3;
        }
        p1 += offset;
    }
    img->UnlockBits(&data);     
}

!

0

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


All Articles