Saving picturebox image to file 
Author Message
 Saving picturebox image to file
Hi,

How do you save an image from a picturebox to a file?

Thanx



Sat, 13 Nov 2004 23:02:53 GMT  
 Saving picturebox image to file
Hi Clint, below is an example that seems to work .. but is C++ code and is
for two colors only ...   hopefully you can use it ..

-BB

BOOL createBitmapFile(LPCTSTR szFileName)
{
    //BITMAP bmp;
    PBITMAPINFO pbmi = NULL;
    WORD    cClrBits = 1;
 int  nColors  = 2;
 RGBQUAD Colors2[] =
 {
  {0x00, 0x00, 0x00, 0x00},
  {0xFF, 0xFF, 0xFF, 0x00}
 };

 int     nWidth    = (int) spPictureBox->GetWidth();
 int     nHeight    = (int) spPictureBox->GetHeight();

 int     nBytesPerLine  = ((nWidth * cClrBits + 31)&(~31)) / 8;
 DWORD    dwBitmapInfoSize = sizeof(BITMAPINFOHEADER) + nColors *
sizeof(RGBQUAD);
 DWORD    dwFileHeaderSize = dwBitmapInfoSize + sizeof(BITMAPFILEHEADER);

 BITMAPFILEHEADER bmpfhdr;
 BYTE*    lpBuf    = new BYTE[dwBitmapInfoSize];
 BITMAPINFO*   lpbi    = (BITMAPINFO*) lpBuf;
 BITMAPINFOHEADER& bih     = lpbi->bmiHeader;

 HBITMAP    hBmp;
 LPVOID    ppvBits;

 memset(&bih, 0, sizeof(BITMAPINFOHEADER));

 // Initialize the fields in the BITMAPINFO structure.
 bih.biSize   = sizeof(BITMAPINFOHEADER);
 bih.biWidth   = nWidth;
 bih.biHeight  = nHeight;
 bih.biPlanes  = 1;
 bih.biBitCount  = 1;
 bih.biCompression = BI_RGB;
 bih.biSizeImage  = 0;

 RGBQUAD*   pColors = &Colors2[0];
 memcpy(&lpbi->bmiColors[0], &pColors[0], sizeof(RGBQUAD));
 memcpy(&lpbi->bmiColors[1], &pColors[1], sizeof(RGBQUAD));

 bmpfhdr.bfType  = 0x4d42;  //'BM' file type
 bmpfhdr.bfSize  = dwFileHeaderSize + (nBytesPerLine * nHeight);
 bmpfhdr.bfReserved1 = 0;
 bmpfhdr.bfReserved2 = 0;
 bmpfhdr.bfOffBits = dwFileHeaderSize;

 //get a memory device context(DC) first...
 m_hPictureBoxDC   = ::GetDC(m_hPictureBox);

 //then our DIBSection using the structure manually created from above...
 m_hPictureBoxBMP = CreateDIBSection(m_hPictureBoxDC, lpbi, DIB_RGB_COLORS,
&ppvBits, NULL, 0);
 m_hMemDC   = CreateCompatibleDC(m_hPictureBoxDC);

 hBmp    = (HBITMAP) SelectObject(m_hMemDC, m_hPictureBoxBMP);
 BitBlt(m_hMemDC,0,0,nWidth,nHeight,m_hPictureBoxDC,0,0,SRCCOPY);
 SelectObject(m_hMemDC, hBmp);

 DWORD dwNumBytes;

 HANDLE hFile = CreateFile(szFileName,
                      GENERIC_WRITE,
             (DWORD) 0, NULL, CREATE_ALWAYS,
          FILE_ATTRIBUTE_NORMAL, NULL);

 if (hFile == INVALID_HANDLE_VALUE)
  return FALSE;

 WriteFile(hFile, &bmpfhdr, sizeof(bmpfhdr), &dwNumBytes, NULL); // Write
the file header
 WriteFile(hFile, lpbi, dwBitmapInfoSize, &dwNumBytes, NULL); // Write the
DIB header
 WriteFile(hFile, ppvBits, nBytesPerLine * nHeight, &dwNumBytes, NULL); //
Write DIB bits
 CloseHandle(hFile);

 delete lpBuf;

 DeleteObject(hBmp);
 DeleteDC(m_hMemDC);
 ::ReleaseDC(m_hPictureBox, m_hPictureBoxDC);

 return TRUE;

Quote:
}



Quote:
> Hi,

> How do you save an image from a picturebox to a file?

> Thanx



Sun, 14 Nov 2004 12:33:55 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Help!! Loading PictureBox FromStream and PictureBox.Image.Save

2. Save An Appended Image file loaded in the PictureBox

3. Save An Appended Image file loaded in the PictureBox

4. Saving Image or PictureBox control's contents to file (BMP/JPG etc)

5. Display Image in picturebox from db image field without temp file

6. Save Image to Database from Picturebox

7. ADVANCED: Saving Picturebox-image as GIF

8. How to save a Picturebox Image

9. Save an image using PictureBox control?

10. Saving an image in a picturebox.

11. Saving the image in an Picturebox to something other then a BMP

12. ADVANCED: Saving Picturebox-image as GIF

 

 
Powered by phpBB® Forum Software