Я отправляю данные изображения из скрипта C # в DLL C ++. Я хотел бы знать, как превратить эти данные обратно в изображение на стороне C ++, чтобы затем их можно было отображать с помощью GDI +.
Код C #
[DllImport("ExtWindow")]
private static extern void SetImage(IntPtr img, int width, int height);
(...)
using (var bmp = new Bitmap(@"D:/my_image.png"))
{
BitmapData data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat);
SetImage(bmp.GetHbitmap(), bmp.Width, bmp.Height);
bmp.UnlockBits(data);
}
Код C ++
Bitmap* display_img;
extern "C" void __declspec(dllexport) SetImage(void* data, int width, int height) {
HBITMAP hBmp = CreateBitmap(width, height, 1, 24, data);
display_img = (Bitmap*)Bitmap::FromHBITMAP(hBmp, NULL); // <=== i fail to properly convert here
// Force WM_PAINT
RedrawWindow(my_hWnd, 0, 0, RDW_INVALIDATE | RDW_UPDATENOW);
}
(...) // A window is created earlier, which is where the image will be displayed
LRESULT CALLBACK newWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
switch (message)
{
case WM_PAINT:
// Display the image
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW + 1));
Graphics graphics(hdc);
graphics.DrawImage(display_img, 0, 0);
EndPaint(hWnd, &ps);
return 0;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}