Abstract因为Computer Vision的作业,之前都是用C# + GDI+写,但这次的作业要做Grayscale Dilation,想用STL的Generic Algorithm写,但C++ Standard Library并无法读取jpg档,用其它Library又比较麻烦,所以又回头想到GDI+,能同时使用STL和GDI+的,也只有C++/CLI了,我想这也是C++/CLI的优势之一,可以让你同时发挥.NET Framework和STL的power,以下的范例demo如何使用C++/CLI配合GDI+将jpg档读进来,并写入jpg檔。
Introduction
此范例比须手动加上Reference System.Drawing.dll 1 /**/ /* 2(C) OOMusou 2006 http://oomusou.cnblogs.com 3 4Filename : ReadJpg.cpp 5Compiler : Visual C++ 8.0 6Description : Demo how to read/write jpg by GDI+ 7Release : 11/19/2006 8*/ 9 10 #include " stdafx.h " 11 12 using namespace System::Drawing; 13 using namespace System::Drawing::Imaging; 14 15 int main() { 16 Bitmap ^originalImage = gcnew Bitmap("lena.jpg");17 Bitmap ^newImage = gcnew Bitmap(originalImage->Width, originalImage->Height);1819 for(int x = 0; x!= originalImage->Width;++x) { 20 for(int y = 0; y != originalImage->Height; ++y) { 21 int r = originalImage->GetPixel(x,y).R;22 int g = originalImage->GetPixel(x,y).G;23 int b = originalImage->GetPixel(x,y).B;2425 newImage->SetPixel(x,y, Color::FromArgb(r, g, b));26 }27 }2829 newImage->Save("newlena.jpg");3031 return 0;32}
See Also