Karim Agha
Karim Agha

Reputation: 3625

GDI+ Bitmap Save problem

Bitmap bff(L"1.jpg");
bff.Save(L"2.jpg", &Gdiplus::ImageFormatJPEG, NULL);

This creates a new file 2.jpg with zero-bytes length. Isn't it supposed to write an image file that is identical to 1.jpg?

Why I'm having zero-bytes length files? I'm doing this test because writing other Bitmaps to files, result in the same output.

Upvotes: 1

Views: 20612

Answers (3)

c00000fd
c00000fd

Reputation: 22307

Here's a fast way to save it, since GetEncoderClsid is a custom function:

//Save to PNG
CLSID pngClsid;
CLSIDFromString(L"{557CF406-1A04-11D3-9A73-0000F81EF32E}", &pngClsid);
bmp.Save(L"file.png", &pngClsid, NULL);

and here's IDs for other formats:

bmp: {557cf400-1a04-11d3-9a73-0000f81ef32e}
jpg: {557cf401-1a04-11d3-9a73-0000f81ef32e}
gif: {557cf402-1a04-11d3-9a73-0000f81ef32e}
tif: {557cf405-1a04-11d3-9a73-0000f81ef32e}
png: {557cf406-1a04-11d3-9a73-0000f81ef32e}

Upvotes: 9

AnT stands with Russia
AnT stands with Russia

Reputation: 320709

AFAIK, you can't just pass the image format GUID ('ImageFormatJPEG' in your case) to 'Image::Save' method. The second argument is supposed to hold the encoder GUID, not a format GUID. See an example here

Upvotes: 2

GRB
GRB

Reputation: 4022

&Gdiplus::ImageFormatJPEG is the wrong value to send as the second parameter (thus why the new file is zero bytes large). Take a look at the code example at the bottom of the Image::Save() reference page which demonstrates the proper usage of Save().

Upvotes: 2

Related Questions