GSI


WebBrowser를 사용해야 되는 부분이 생겨서
많이 자료를 보고 해봤지만 TopMost라는 특성 때문에 Winform, WPF 모두
원하는 효과를 낼수가 없더라구요.

그래서 여러가지 보고 테스트도 해봤지만.
어떤건 속도가 너무 느리더라구요.

아래 코드는 완전 해결한건 아니지만,
이것도 조금은 접근이 된거 같아요.

목표점은
- 스레드를 통한 백그라운드 처리를 통해서 다른 UI의 동작을 원할하게 처리
-  모자이크 처리로 화면에 출력함으로 해서 조금은 이펙트를 중점으로 한다.

아래의 코드는 일부 접근된 코드를 우선 올려 봅니다.

-- Code --
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Media;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        WebBrowser web = new WebBrowser();

        public Form1()
        {
            InitializeComponent();

            web.Width = 1000;
            web.Height = 1000;
            web.ScrollBarsEnabled = false;
            web.ScriptErrorsSuppressed = true;
            web.Navigate("http://dev.iamgsi.com/googlemap");

            timer1.Start();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //while (web.ReadyState != WebBrowserReadyState.Complete)
            //    System.Windows.Forms.Application.DoEvents();
            //System.Threading.Thread.Sleep(TimeSpan.FromSeconds(5));

            int width = web.Document.Body.ScrollRectangle.Width;
            int height = web.Document.Body.ScrollRectangle.Height;
            web.Width = width;
            web.Height = height;

            System.Drawing.Bitmap bmp = new Bitmap(width, height);
            web.DrawToBitmap(bmp, new System.Drawing.Rectangle(0, 0, width, height));

            this.pictureBox1.Width = width;
            this.pictureBox1.Height = height;
            if (this.pictureBox1.Image != null)
            {
                this.pictureBox1.Image.Dispose();
            }
            this.pictureBox1.Image = null;
            this.pictureBox1.Image = bmp;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            web.Document.InvokeScript("MoveAddress", new object[] { "서울" });
        }

        private void button3_Click(object sender, EventArgs e)
        {
            web.Document.InvokeScript("MoveAddress", new object[] { "거창" });
        }

        private void button4_Click(object sender, EventArgs e)
        {
            web.Document.InvokeScript("MoveAddress", new object[] { "광주" });
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (web.ReadyState != WebBrowserReadyState.Complete)
                return;

            int width = web.Document.Body.ScrollRectangle.Width;
            int height = web.Document.Body.ScrollRectangle.Height;
            web.Width = width;
            web.Height = height;

            System.Drawing.Bitmap bmp = new Bitmap(width, height);
            web.DrawToBitmap(bmp, new System.Drawing.Rectangle(0, 0, width, height));

            this.pictureBox1.Width = width;
            this.pictureBox1.Height = height;
            if (this.pictureBox1.Image != null)
            {
                this.pictureBox1.Image.Dispose();
            }
            this.pictureBox1.Image = null;
            this.pictureBox1.Image = bmp;
        }
    }
}

Posted by gsi
:

사용자 삽입 이미지

이미지는 웹 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++;
}

Posted by gsi
:

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);   
}

Posted by gsi
: