{"id":528,"date":"2015-07-10T10:28:30","date_gmt":"2015-07-10T00:28:30","guid":{"rendered":"http:\/\/www.nickdu.com\/?p=528"},"modified":"2015-07-10T10:28:30","modified_gmt":"2015-07-10T00:28:30","slug":"convert-dib-or-bmp-into-jpeg-in-memory-diskless-using-windows-gdiplus","status":"publish","type":"post","link":"https:\/\/nickdu.com\/?p=528","title":{"rendered":"Convert DIB or BMP into JPEG in memory (diskless) using Windows GDIPlus"},"content":{"rendered":"<pre lang=\"cpp\">    \n    #include <gdiplus.h>\n    #include <GdiPlusImageCodec.h>\n\n    using namespace Gdiplus;\n\n    GdiplusStartupInput gdiplusStartupInput;\n    ULONG_PTR gdiplusToken;\n    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);\n    ...\n    IStream* pJpegStream = NULL;  \/\/declare a memory stream\n    CreateStreamOnHGlobal(NULL, TRUE, (LPSTREAM*)&amp;pJpegStream);\n\n    GpBitmap* pBitmap = NULL;\n    unsigned char* dib = GetDib(); \/\/get DIB or BMP data buffer\n    DllExports::GdipCreateBitmapFromGdiDib((LPBITMAPINFO)dib, dib + sizeof(BITMAPINFOHEADER), &amp;pBitmap);\n\n    CLSID imageCLSID;\n    GetEncoderClsid(L\"image\/jpeg\", &amp;imageCLSID);\n\n    int jpegQuality = 100;\n\n    EncoderParameters encoderParams; \/\/setup jpeg encoder parameters\n    encoderParams.Count = 1;\n    encoderParams.Parameter[0].NumberOfValues = 1;\n    encoderParams.Parameter[0].Guid = EncoderQuality;\n    encoderParams.Parameter[0].Type = EncoderParameterValueTypeLong;\n    encoderParams.Parameter[0].Value = &amp;jpegQuality;\n\n    \/\/save jpeg into memory stream\n    DllExports::GdipSaveImageToStream(pBitmap, pJpegStream, &amp;imageCLSID, &amp;encoderParams);\n\n    LARGE_INTEGER lnOffset;\n    lnOffset.QuadPart = 0;\n    ULARGE_INTEGER ulnSize;\n    \/\/determine memory stream length\n    pJpegStream-&gt;Seek(lnOffset, STREAM_SEEK_END, &amp;ulnSize);\n    pJpegStream-&gt;Seek(lnOffset, STREAM_SEEK_SET, NULL);\n    int encodedBytes = ulnSize.QuadPart;\n    \n    \/\/store jpeg memory stream into jpeg data buffer\n    pJpegStream-&gt;Read(racBuf_, ulnSize.QuadPart, NULL);\n    ...\n    GdiplusShutdown(gdiplusToken);\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>#include #include using namespace Gdiplus; GdiplusStartupInput gdiplusStartupInput; ULONG_PTR gdiplusToken; GdiplusStartup(&#038;gdiplusToken, &#038;gdiplusStartupInput, NULL); &#8230; IStream* pJpegStream = NULL; \/\/declare a memory stream CreateStreamOnHGlobal(NULL, TRUE, (LPSTREAM*)&amp;pJpegStream); GpBitmap* pBitmap = NULL; unsigned char* dib = GetDib(); \/\/get DIB or BMP data buffer DllExports::GdipCreateBitmapFromGdiDib((LPBITMAPINFO)dib, dib + sizeof(BITMAPINFOHEADER), &amp;pBitmap); CLSID imageCLSID; GetEncoderClsid(L&#8221;image\/jpeg&#8221;, &amp;imageCLSID); int jpegQuality = 100; EncoderParameters encoderParams; \/\/setup &hellip; <a href=\"https:\/\/nickdu.com\/?p=528\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Convert DIB or BMP into JPEG in memory (diskless) using Windows GDIPlus&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,2],"tags":[],"class_list":["post-528","post","type-post","status-publish","format-standard","hentry","category-cc","category-it"],"_links":{"self":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/posts\/528","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=528"}],"version-history":[{"count":0,"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/posts\/528\/revisions"}],"wp:attachment":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=528"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=528"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=528"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}