瀏覽代碼

mat转gid+ Bitmap;

scbc.sat2 5 年之前
父節點
當前提交
2472094a40
共有 2 個文件被更改,包括 23 次插入1 次删除
  1. 22 1
      cv/cv/cv.cpp
  2. 1 0
      cv/cv/framework.h

+ 22 - 1
cv/cv/cv.cpp

@@ -626,5 +626,26 @@ CV_API bool HBitmap2Mat(HBITMAP& bitmap, cv::Mat& img)
 
 CV_API bool GdiplusBitmap2Mat(Gdiplus::Bitmap& bitmap, cv::Mat& img)
 {
-	return CV_API bool();
+	Gdiplus::PixelFormat format = bitmap.GetPixelFormat();
+	if (format != PixelFormat24bppRGB)
+		return false;
+
+	int width = bitmap.GetWidth();
+	int height = bitmap.GetHeight();
+
+	Gdiplus::Rect rcLock(0,0,width,height);
+	Gdiplus::BitmapData bmpData;
+
+	if ( !bitmap.LockBits(&rcLock, Gdiplus::ImageLockModeRead, format, &bmpData) == Gdiplus::Ok )
+	{
+		return false;
+	}
+
+	img = cv::Mat(height, width, CV_8UC3, static_cast<unsigned char*>(bmpData.Scan0), bmpData.Stride).clone();
+	bitmap.UnlockBits(&bmpData);
+
+	if (img.empty())
+		return false;
+
+	return true;
 }

+ 1 - 0
cv/cv/framework.h

@@ -36,3 +36,4 @@
 #endif
 
 #include <gdiplus.h>
+#pragma comment(lib, "gdiplus.lib")