Home All Groups Group Topic Archive Search About

Saving a locally created Bitmap

Author
24 Aug 2006 4:37 PM
algorimancer
Seems like this ought to be an easy one.  Essentially, using managed
C++, I'd like to be able to create a Bitmap locally, set the pixels to
particular colors, and save the result.  Everything seems to work fine
except that upon calling the Bitmap's Save function I always get a
status of "FileNotFound" and it creates a file with 0 size.

The Code:

void CChildView::Test()
{
    Gdiplus::Status stat;

    Bitmap bmp(2048,2048,PixelFormat24bppRGB);

    Color red,grn;
    red.SetFromCOLORREF(RGB(255,0,0));
    grn.SetFromCOLORREF(RGB(0,255,0));
    int i,j;
    for(i=0;i<2048;++i)
    {
        for(j=0;j<2048;++j)
        {


            if(i==j)
                stat=bmp.SetPixel(i,j,grn);
            else
                stat=bmp.SetPixel(i,j,red);
        }
    }

    CString sPath("c:\\test.png");
    WCHAR wszPath[256];
    MultiByteToWideChar( CP_ACP, 0, sPath,
        sPath.GetLength()+1, wszPath,
        sizeof(wszPath)/sizeof(wszPath[0]) );

    stat=bmp.Save(wszPath,&ImageFormatPNG);
}

I should mention that yes, Gdiplus is initialized prior to entering
this function.  Status (stat) remains "OK" until the Save operation,
where it changes to "FileNotFound".  What am I missing here?  I've had
the same problem with different file types (BMP rather than PNG, for
instance).

AddThis Social Bookmark Button