2014-12-27 43 views
1

如何在C++/CLI中將圖像從圖片框轉換爲:: MAT轉換系統::圖::位圖^到:: MAT

謝謝

+0

也許你想用'LockBits'來得到一個指向像素值數組的指針。像這樣:http://stackoverflow.com/q/24655452/103167 –

+0

不行,因爲這行'Gdiplus :: Bitmap * enhanced = pictureBox1-> Image;',給出這個錯誤:_IntelliSense:一個類型的值「System :: Drawing :: Image ^」不能用於初始化類型爲「Gdiplus :: Bitmap *」的實體_ –

+0

當然,您應該使用C++/CLI語法。 'System :: Drawing :: Bitmap'是圍繞本地類'Gdiplus :: Bitmap'的.NET包裝。 –

回答

1

您需要cast的Drawing.Image成Bitmap(假設該圖像確實是一個位圖)。
然後鎖定System.Drawing.Bitmap,並使用BitmapData的Scan0屬性訪問內部緩衝區。

System::Drawing::Bitmap^bitmapFrame = safe_cast< System::Drawing::Bitmap^>(pictureBox1->Image); 

BitmapData^ bmpData = bitmapFrame->LockBits(gcnew Rectangle(0, 0, bitmapFrame->Width, bitmapFrame->Height), System::Drawing::Imaging::ImageLockMode::ReadWrite, 
      bitmapFrame->Format); 
try 
{  
    void* data = bmpData.Scan0; 

    //use the data in the ::Mat constructor. 
} 
finally { bitmapFrame->UnlockBits(bmpData); }//Remember to unlock!!!