'bitmap'에 해당되는 글 2건

  1. 2008/01/07 [C#, WinForm] ImageList 채우기 및 ListView 와 연동하기 (Stream, ListViewItem)
  2. 2007/12/21 [MFC] - 화면 DC 를 Bitmap로 저장하는 코드...(펌)
2008/01/07 14:40

[C#, WinForm] ImageList 채우기 및 ListView 와 연동하기 (Stream, ListViewItem)

사용자 삽입 이미지

이미지는 웹 iis 쪽에 있으며 그 이미지를 ImageList에 Form_Load() 시에 추가하고 Listview에 연결한 후에 item을 추가 한 코드 예제 입니다.

string[] filepathlist =
{
"http://localhost:8888/_01.gif",
"http://localhost:8888/_01.gif",
"http://localhost:8888/_02.gif",
"http://localhost:8888/_03.gif",
};

int count = 0; // Item 이름을 위한 임시 변수
foreach (string path in filepathlist)
{
    // WebClient 를 사용해서 원격 이미지를 로드 하고 Stream에 Write 한다.
    WebClient client = new WebClient();
    byte[] myDataBuffer = client.DownloadData(path);
    Stream stream = new MemoryStream();
    stream.Write(myDataBuffer, 0, myDataBuffer.Length);

    // Bitmap에 Stream을 입력 하고 ImageList에 등록한다.
    Bitmap bmp = new Bitmap(stream);
    this.imageList1.Images.Add(bmp);
    int idx = this.imageList1.Images.Keys.Count - 1;
    this.imageList1.Images.SetKeyName(idx, "");

    // ListViewItem에 값을 채우고 ListView에 추가 한다.
    ListViewItem listviewitem = new ListViewItem("test" + count.ToString(), idx);
    this.listView1.Items.Add(listviewitem);

    count++;
}

크리에이티브 커먼즈 라이선스
Creative Commons License
Trackback 0 Comment 0
2007/12/21 12:45

[MFC] - 화면 DC 를 Bitmap로 저장하는 코드...(펌)

void CChildView::SaveBitmapToDirectFile(CDC* pDC, CRect BitmapSize, int BitCount, CString strFilePath)
{
 CBitmap bmp, *pOldBmp;
 CDC dcMem; 
 BITMAP                  bm;
 BITMAPINFOHEADER        bi;
 LPBITMAPINFOHEADER      lpbi;
 DWORD                   dwLen;
 HANDLE                  handle;
 HANDLE                  hDIB;  
 HPALETTE                hPal=NULL;

 /*----- CDC의 내용을 Bitmap으로 전송 ----*/
 dcMem.CreateCompatibleDC(pDC);
 bmp.CreateCompatibleBitmap(pDC ,BitmapSize.Width(),BitmapSize.Height());   
 pOldBmp = (CBitmap*) dcMem.SelectObject(&bmp);
 dcMem.BitBlt( 0,0, BitmapSize.Width(), BitmapSize.Height(), pDC, 0,0, SRCCOPY);
 dcMem.SelectObject(pOldBmp);

 if (strFilePath == "")          return;
 /*------------------------- 비트멥 헤더를 기록함 -------------------------*/
 if (hPal==NULL)
  hPal = (HPALETTE) GetStockObject(DEFAULT_PALETTE);
 GetObject(HBITMAP(bmp), sizeof(BITMAP), &bm);

 bi.biSize               = sizeof(BITMAPINFOHEADER);
 bi.biWidth              = bm.bmWidth;
 bi.biHeight             = bm.bmHeight;
 bi.biPlanes             = 1;
 bi.biBitCount           = 32;      
 bi.biCompression        = BI_RGB;
 bi.biSizeImage          = bm.bmWidth * bm.bmHeight * 3;
 bi.biXPelsPerMeter      = 0;
 bi.biYPelsPerMeter      = 0;
 bi.biClrUsed            = 0;
 bi.biClrImportant       = 0;

 int nColors = (1 << bi.biBitCount);
 if( nColors > 256 )
  nColors = 0;
 dwLen  = bi.biSize + nColors * sizeof(RGBQUAD);
 hPal = SelectPalette(pDC->GetSafeHdc(),hPal,FALSE);
 RealizePalette(pDC->GetSafeHdc());
 hDIB = GlobalAlloc(GMEM_FIXED,dwLen);
 lpbi = (LPBITMAPINFOHEADER)hDIB;
 *lpbi = bi;
 GetDIBits(pDC->GetSafeHdc(),
  HBITMAP(bmp),
  0,
  (DWORD)bi.biHeight,
  (LPBYTE)NULL,
  (LPBITMAPINFO)lpbi,
  (DWORD)DIB_RGB_COLORS);
 bi = *lpbi;
 if (bi.biSizeImage == 0)
 {
  bi.biSizeImage = ((((bi.biWidth * bi.biBitCount) + 31) & ~31) / 8)
   * bi.biHeight;
 }
 dwLen += bi.biSizeImage;
 if (handle = GlobalReAlloc(hDIB, dwLen, GMEM_MOVEABLE))
  hDIB = handle;

 lpbi = (LPBITMAPINFOHEADER)hDIB;
 GetDIBits(pDC->GetSafeHdc(),
  HBITMAP(bmp),
  0,                   
  (DWORD)bi.biHeight,     
  (LPBYTE)lpbi       
  + (bi.biSize + nColors * sizeof(RGBQUAD)),
  (LPBITMAPINFO)lpbi,  
  (DWORD)DIB_RGB_COLORS);

 BITMAPFILEHEADER      hdr;
 hdr.bfType        = ((WORD) ('M' << 8) | 'B');       
 hdr.bfSize        = GlobalSize (hDIB) + sizeof(hdr);  
 hdr.bfReserved1   = 0;                                
 hdr.bfReserved2   = 0;                                
 hdr.bfOffBits=(DWORD)(sizeof(hdr)+lpbi->biSize + nColors * sizeof(RGBQUAD));
 char* pBmpBuf;
 DWORD FileSize;
 FileSize=sizeof(hdr)+GlobalSize(hDIB);
 pBmpBuf = new char[FileSize];
 memcpy(pBmpBuf,&hdr,sizeof(hdr));
 memcpy(pBmpBuf+sizeof(hdr),lpbi,GlobalSize(hDIB));
 /*--------------------- 실제 파일에 기록함 --------------------------*/
 CFile file;
 file.Open(strFilePath, CFile::modeCreate | CFile::modeNoTruncate | CFile::modeWrite);
 file.Write(pBmpBuf,FileSize);
 file.Close();
 /*------------------------ 임시로 할당한 메모리를 해제.. -------------*/
 delete[] pBmpBuf;

 if(hDIB)
 {      
  GlobalFree(hDIB);
 }
 SelectPalette(pDC->GetSafeHdc(),hPal,FALSE);   
}

크리에이티브 커먼즈 라이선스
Creative Commons License
Trackback 0 Comment 0