Microsoft MVP성태의 닷넷 이야기
C++ Dll 에서 C# 의 PictureBox이미지 변경문제 [링크 복사], [링크+제목 복사],
조회: 18528
글쓴 사람
박주만 (ubdown at nate.com)
홈페이지
첨부 파일
 

안녕하세요...

C# 의 PictureBox 이미지를 C++ DLL 에서 바꾸는 문제 입니다.

몇일째 찾아 보다가 도저히 답이 안나와서 문의 드리게 되었네요...

우선 개발 환경은 :
OS : window8.1
Tool : Visual Studio 2012

Code 의 주석으로 설명할께요...

//----------------------------------------------------------------------

Main.C#

//C++ DLL 호출
  [DllImport("CameraDll.dll")]
  static extern void dll_Button1_Click(IntPtr hwnd, int deviceIndex);

private void Button1_Click(object sender, EventArgs e)
{
  IntPtr inpt2 = pictureBox1.Handle;
  dll_Button1_Click( inpt2 , g_DeviceIndex);
}


//----------------------------------------------------------------------




CameraDll.CPP DLL

unsigned char*        m_pDisplayBuffer;
unsigned char* m_ppDisplayBuffer[2];
HWND hwnd;



////////////////////////////////////////////////////////////////////////////////////////////////
//    Display Image
////////////////////////////////////////////////////////////////////////////////////////////////
void DisplayImage(HDC hDC, int width, int height, int dw, int dh, unsigned char*pImage)
{
    HDC MemDC;
    BITMAPINFO        bmpInfo;
    BITMAPFILEHEADER bmFileHeader;
    BITMAP Bitmap;

    bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    bmpInfo.bmiHeader.biWidth = width;
    bmpInfo.bmiHeader.biHeight = height;
    bmpInfo.bmiHeader.biPlanes = 1;
    bmpInfo.bmiHeader.biBitCount = 24;
    bmpInfo.bmiHeader.biCompression = BI_RGB ;
    bmpInfo.bmiHeader.biSizeImage = 0;
    bmpInfo.bmiHeader.biXPelsPerMeter = 0;
    bmpInfo.bmiHeader.biYPelsPerMeter = 0;
    bmpInfo.bmiHeader.biClrUsed = 0;
    bmpInfo.bmiHeader.biClrImportant = 0;

    if(width == dw)
    {
        MemDC = CreateCompatibleDC(hDC); //<--- hdc : C#의 PictureBox HDC
        printf("Create BitMap\n");
    //    BitMapByteCallBackHandler(&h_bitmap); //<-- C#으로 이미지 Data를 가져와서 처리 하려고 시도...
                                                                         // C#의 PictureBox 이미지를 변경 안됨!!!!!!! 실행시 오류 없이 진행 됨, Debug 오류 없음..
        SetDIBitsToDevice(MemDC,    0, 0,    (width), (height),    0, 0, 0, (height),    pImage,    &bmpInfo, DIB_RGB_COLORS); //<--- WinAPI에서 실행할 경우 변경됨


        DeleteDC(MemDC);
    }
    else
    {
        
    }
}

void MakeDisplayImage(int width, int height, unsigned char* pSrc, unsigned char* pDst)
{
    int wbytes;
    wbytes = width * 3;

    for(int i = 0; i < height; i++)
    {
        for(int j = 0; j < width; j++)
        {
            pDst[i * wbytes + j * 3] = pSrc[((height - 1) - i) * width + ((width - 1) - j)];
            pDst[i * wbytes + j * 3 + 1] = pSrc[((height - 1) - i) * width + ((width - 1) - j)];
            pDst[i * wbytes + j * 3 + 2] = pSrc[((height - 1) - i) * width + ((width - 1) - j)];
        }
    }
}

void CALLBACK OnTimer(HWND fhwnd, UINT uMsg, UINT idEvent, DWORD dwTime)
{
    int errorCode = 0;
    unsigned char*    pAddress;

    errorCode = UCamGetImageData(g_DeviceIndex, &pAddress); //<--- pAddress 를 통해 Display 할 이미지 Dataf가져옴
    if(errorCode == UCAM_NEW_FRAME)
    {
        SetWindowText(hwnd,"Test"); // <-- C# 의 Button Text 변경 X 안됨
        EnableWindow(hwnd, FALSE); // <-- C# 의 Button Enable 변경 O 됨
        
                HDC hdc = GetDC(hwnd);
        MakeDisplayImage(m_nWidth, m_nHeight, pAddress, m_ppDisplayBuffer[g_DisplayIndex]); // <--- ppDisplayBuffer

        DisplayImage(hdc, m_nWidth, m_nHeight, m_nDisplayWidth, m_nDisplayHeight, m_ppDisplayBuffer[g_DisplayIndex]); <---- 이미지를 Display 호출 ****

        //m_PictureControl.ReleaseDC(hdc);
        g_DisplayIndex++;
        if(g_DisplayIndex == DISPLAY_BUFFERCOUNT)
        {
            g_DisplayIndex = 0;
        }
    }else{
        printf("-----------------------\n");
    }

}

extern "C" __declspec(dllexport) void __cdecl dll_IDCUCAMSTART_Click(HWND fhwnd, int deviceIndex){ //<----- C#
    hwnd = fhwnd;
    
    BufferInitialize();
    USBReceiveCallback(USBReceiveHandler);
    SetTimer(NULL, ID_TIMER101, 500, OnTimer);

    m_ReceiveMode = 1;
    UCamStart(g_DeviceIndex, m_ReceiveMode);
    m_bDisplayMode = false;
}


//----------------------------------------------------

DisplayImage 함수에서 CallBack 으로

C++ Dll

typedef void (__cdecl * BitMapByteCallback)(const HBITMAP fbyte);

HBITMAP OldBitmap;
HBITMAP hBitmap;
BITMAP bit;
int bx,by;

MemDC = CreateCompatibleDC(hDC); //<--- hdc : C#의 PictureBox HDC

hBitmap = CreateDIBitmap(MemDC,(BITMAPINFOHEADER *)&bmpInfo,CBM_INIT, (PBYTE)pImage,(BITMAPINFO *)&bmpInfo,DIB_RGB_COLORS);
        
OldBitmap = (HBITMAP)SelectObject(MemDC, &hBitmap);

BitMapByteCallBackHandler(OldBitmap); <-- Callback C# Function 호출

C# CallBack

public void BitMapByteCallBackHandler(IntPtr hBitmap)
{
            
            Bitmap bitmap = null;
            try
            {
                bitmap = new Bitmap(Image.FromHbitmap(hBitmap)); // <-- System.Runtime.InteropServices.ExternalException (0x80004005): GDI+에서 일반 오류가 발생했습니다.
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
}

왜 System.Runtime.InteropServices.ExternalException (0x80004005): GDI+에서 일반 오류가 발생했습니다. 발생할까요???





요약:
1. C++ DLL 에서 C# 의 PictureBox의 이미지를 변경 하고 싶습니다.
2. BitMapByteCallBackHandler를 이용하여 C#으로 HBitMap 이미지를 보내서
   C#에서 구현 하려고 한다면 어떻게 해야 할까요??
3. System.Runtime.InteropServices.ExternalException
     








[최초 등록일: ]
[최종 수정일: 12/19/2013]


비밀번호

댓글 작성자
 



2013-12-19 04시30분
HBITMAP이 PVOID 값이니, C++에서 넘기기 전의 값과 C#에서 받은 IntPtr의 값이 동일한지 확인해 보세요. 딱히, 위의 소스코드만 봐서는 별다른 오류가 없어보이는데요.

다른 소스코드는 다 떼어버리고 문제가 재현되는 최소한의 코드를 구성해서 프로젝트로 파일로 올려주시면 확인해 보겠습니다.
정성태

... [91]  92  93  94  95  96 
NoWriterDateCnt.TitleFile(s)
155최성우4/18/20058054[질문(--),(__)]BHO로 자동 로그인 기능 구현.. 패스워드를 읽어오지를 못합니다.
156정성태4/19/20056993    답변글 [답변]: [질문(--),(__)]BHO로 자동 로그인 기능 구현.. 패스워드를 읽어오지를 못합니다.
161최성우4/21/20057837        답변글 [답변]: [답변]: [질문(--),(__)]BHO로 자동 로그인 기능 구현.. 패스워드를 읽어오지를 못합니다.
164정성태4/22/20056776            답변글 [답변]: [답변]: [답변]: [질문(--),(__)]BHO로 자동 로그인 기능 구현.. 패스워드를 읽어오지를 못합니다.
176최성우5/3/20057183                답변글 [답변]: [답변]: [답변]: [답변]: [질문(--),(__)]BHO로 자동 로그인 기능 구현.. 패스워드를 읽어오지를 못합니다.
146안연준4/14/20057959컴포넌트 안에 컴포넌트 삽입? 헐 ! -_-;;
147정성태4/14/20056827    답변글 [답변]: 컴포넌트 안에 컴포넌트 삽입? 헐 ! -_-;;
148안연준4/15/20057135        답변글 [답변]: [답변]: 컴포넌트 안에 컴포넌트 삽입? 헐 ! -_-;; [2]
142김용국4/13/20057022SmartClient 방식에서 이미지(바이너리)파일을 DataBased에 저장하기위한 방안에 대한 문의
143정성태4/14/20056626    답변글 [답변]: SmartClient 방식에서 이미지(바이너리)파일을 DataBased에 저장하기위한 방안에 대한 문의
141김종욱4/12/20057002웹하드 시스템을 ACTIVEX 로 짜고 있습니다
144정성태4/14/20056783    답변글 [답변]: 웹하드 시스템을 ACTIVEX 로 짜고 있습니다
149김종욱4/15/20056472        답변글 [답변]: [답변]: 웹하드 시스템을 ACTIVEX 로 짜고 있습니다 [1]
150정성태4/15/20056804            답변글 [답변]: [답변]: [답변]: 웹하드 시스템을 ACTIVEX 로 짜고 있습니다 [1]
151김종욱4/16/20056959                답변글 [답변]: [답변]: [답변]: [답변]: 웹하드 시스템을 ACTIVEX 로 짜고 있습니다
152정성태4/16/20056847                    답변글 [답변]: [답변]: [답변]: [답변]: [답변]: 웹하드 시스템을 ACTIVEX 로 짜고 있습니다
153김종욱4/18/20058524                        답변글 [답변]: [답변]: [답변]: [답변]: [답변]: [답변]: 웹하드 시스템을 ACTIVEX 로 짜고 있습니다
154정성태4/18/20057881                            답변글 [답변]: [답변]: [답변]: [답변]: [답변]: [답변]: [답변]: 웹하드 시스템을 ACTIVEX 로 짜고 있습니다 [3]
140안연준4/11/20056709Smart Client 에서 오프라인 글 중...의문점
145정성태4/14/20056479    답변글 [답변]: Smart Client 에서 오프라인 글 중...의문점 [1]
134김용국4/6/200511012c# .Net 에 대한 문의좀 ^^ [WinForm 에서 UserControl로 작성된 폼을 호출하려는데....]
135정성태4/6/20059113    답변글 [답변]: c# .Net 에 대한 문의좀 ^^ [WinForm 에서 UserControl로 작성된 폼을 호출하려는데....]
136김용국4/6/20057737        답변글 [답변]: [답변]: c# .Net 에 대한 문의좀 ^^ [WinForm 에서 UserControl로 작성된 폼을 호출하려는데....]
137정성태4/7/20056986            답변글 [답변]: [답변]: [답변]: c# .Net 에 대한 문의좀 ^^ [WinForm 에서 UserControl로 작성된 폼을 호출하려는데....]
138김용국4/7/20056005                답변글 [답변]: [답변]: [답변]: [답변]: c# .Net 에 대한 문의좀 ^^ [WinForm 에서 UserControl로 작성된 폼을 호출하려는데....]
139김용국4/11/20056416                    답변글 잘 해결 되었습니다... 감사합니다 [한줄답변]
... [91]  92  93  94  95  96