Saving a JPEG snapshot as a BYTE buffer in C

In one of my previous questions, I asked how to take a screenshot and save it in JPEG format without using GDI + due to restrictions on using only C. At the end, I myself answered the question with some comments. Using a very sophisticated version of C GDI + (loaded at runtime), I can take a screenshot and save it as a JPEG to a file. Now, how would I save the same screenshot not to a file, but to a buffer? unsigned char * buffer? here is the code that needs to be converted.

int SaveJpeg(HBITMAP hBmp, LPCWSTR lpszFilename, ULONG uQuality) { ULONG *pBitmap = NULL; CLSID imageCLSID; EncoderParameters encoderParams; int iRes = 0; typedef Status (WINAPI *pGdipCreateBitmapFromHBITMAP)(HBITMAP, HPALETTE, ULONG**); pGdipCreateBitmapFromHBITMAP lGdipCreateBitmapFromHBITMAP; typedef Status (WINAPI *pGdipSaveImageToFile)(ULONG *, const WCHAR*, const CLSID*, const EncoderParameters*); pGdipSaveImageToFile lGdipSaveImageToFile; // load GdipCreateBitmapFromHBITMAP lGdipCreateBitmapFromHBITMAP = (pGdipCreateBitmapFromHBITMAP)GetProcAddress(hModuleThread, "GdipCreateBitmapFromHBITMAP"); if(lGdipCreateBitmapFromHBITMAP == NULL) { // error return 0; } // load GdipSaveImageToFile lGdipSaveImageToFile = (pGdipSaveImageToFile)GetProcAddress(hModuleThread, "GdipSaveImageToFile"); if(lGdipSaveImageToFile == NULL) { // error return 0; } lGdipCreateBitmapFromHBITMAP(hBmp, NULL, &pBitmap); iRes = GetEncoderClsid(L"image/jpeg", &imageCLSID); if(iRes == -1) { // error return 0; } encoderParams.Count = 1; encoderParams.Parameter[0].NumberOfValues = 1; encoderParams.Parameter[0].Guid = EncoderQuality; encoderParams.Parameter[0].Type = EncoderParameterValueTypeLong; encoderParams.Parameter[0].Value = &uQuality; lGdipSaveImageToFile(pBitmap, lpszFilename, &imageCLSID, &encoderParams); return 1; } 

Thanks for the help.

+4
source share
1 answer

Instead of calling GdipSaveImageToFile you should use GdipSaveImageToStream. This will allow you to save the image in the stream directly, instead of writing it to a file.

See the GDI Image Functions for details.

To create an IStream in memory, you can use CreateStreamOnHGlobal . This allows IStream to be allocated its own memory or to use a pre-allocated buffer.

+3
source

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


All Articles