(原創) 如何使用C++/CLI读/写jpg檔? (.NET) (C++/CLI) (GDI+) (C/C++) (Image Processing)
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
4
Filename : ReadJpg.cpp
5
Compiler : Visual C++ 8.0
6
Description : Demo how to read/write jpg by GDI+
7
Release : 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);
18
19
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;
24
25
newImage->SetPixel(x,y, Color::FromArgb(r, g, b));
26
}
27
}
28
29
newImage->Save("newlena.jpg");
30
31
return 0;
32
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

See Also
(原創) 如何使用ANSI C/ISO C++讀寫24位元的bmp圖檔? (初級) (C/C++)
(原創) 如何使用ANSI C讀寫32位元的BMP圖檔? (C/C++) (C) (Image Processing)
(原創) 如何使用ANSI C讀寫24/32位元的BMP圖檔? (C/C++) (C) (Image Processing)
(原創) 如何用程序的方式载入jpg图形文件? (.NET) (GDI+) (ASP.NET) (Image Processing)