(原創) 如何將圖片上下翻轉? (.NET) (ASP.NET) (GDI+) (Image Processing)
Abstract
這是我修Computer Vision的作業,此原始程式碼也示範出如何Pixel By Pixel的方式編輯圖片以及如何讀取indexd的bmp圖片格式。
Introduction
1
<%@ Page Language="C#" %>
2
3
<%@ Import Namespace="System.Drawing" %>
4
<%@ Import Namespace="System.Drawing.Imaging" %>
5
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
6
7
<script runat="server">
8
/// <summary>
9
/// Upside-down lena.bmp
10
/// </summary>
11
/// <remarks>
12
/// .NET Framework 2.0 can't draw indexd pixel format directly.
13
/// and will get
14
/// "A Graphics object cannot be created from an image that "
15
/// has an indexed pixel format." exception.
16
/// So we have to make a temporary bitmap, and manually draw
17
/// indexed pixel format to general bitmap.
18
///</remarks>
19
protected void Page_Load(object sender, EventArgs e) {
20
// Bitmap uses System.Drawing namespace.
21
Bitmap bmp = new Bitmap(Server.MapPath("lena.bmp"));
22
23
// Size the _bmp to original bitmap's dimension.
24
Bitmap _bmp = new Bitmap(bmp.Width, bmp.Height);
25
26
// Uses temp _bmp to write on canvas.
27
Graphics canvas = Graphics.FromImage(_bmp);
28
29
// Draw the original indexed bitmap's content to the temp _bmp.
30
// Paint the entire region of original bitmap to the temp _bmp.
31
// Use the rectangle type to select area of source image.
32
canvas.DrawImage(bmp, new Rectangle(0, 0, _bmp.Width, _bmp.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel);
33
34
// Make temp _bmp to general bmp.
35
bmp = _bmp;
36
37
// Make a new bitmap for Upside-down.
38
Bitmap bmpUpsideDown = new Bitmap(bmp.Width, bmp.Height);
39
40
int r = 0, g = 0, b = 0; // R,G,B for Original bmp's RGB value.
41
int avgY = (0 + bmp.Height) / 2; // avgY for average Y-axle value.
42
int tarY = 0; // tarY for Upside-down Y-axle value.
43
44
// Pixel by pixcel image processing.
45
for (int x = 0; x < bmp.Width; x++) {
46
for (int y = 0; y < bmp.Height; y++) {
47
// Get RGB from original bitmap.
48
r = bmp.GetPixel(x, y).R;
49
g = bmp.GetPixel(x, y).G;
50
b = bmp.GetPixel(x, y).B;
51
52
// Cause (Origal y + Target y) /2 = Average y.
53
tarY = 2 * avgY - y;
54
55
// Write to new Upsite-down bitmap on specified pixel and RGB.
56
bmpUpsideDown.SetPixel(x, tarY-1, Color.FromArgb(r, g, b));
57
}
58
}
59
60
// Specify HTML's content type.
61
Response.Clear();
62
Response.ContentType = "image/bmp";
63
64
// ImageFormat uses System.Drawing.Imaging namespace.
65
// Must use ImageFormat.Jpeg. If use ImageFormat.Bmp,
66
// you'll get "System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+." error.
67
bmpUpsideDown.Save(Response.OutputStream, ImageFormat.Jpeg);
68
69
// You should always call the Dispose method to release
70
// the Graphics and related resources created by the
71
// FromImage method.
72
_bmp.Dispose();
73
bmp.Dispose();
74
bmpUpsideDown.Dispose();
75
canvas.Dispose();
76
}
77
</script>
78
79
<html xmlns="http://www.w3.org/1999/xhtml">
80
<head runat="server">
81
<title>Untitled Page</title>
82
</head>
83
<body>
84
<form id="form1" runat="server">
85
<div>
86
</div>
87
</form>
88
</body>
89
</html>
90
<%@ Page Language="C#" %>2

3
<%@ Import Namespace="System.Drawing" %>4
<%@ Import Namespace="System.Drawing.Imaging" %>5
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">6

7
<script runat="server">8
/// <summary>9
/// Upside-down lena.bmp 10
/// </summary>11
/// <remarks>12
/// .NET Framework 2.0 can't draw indexd pixel format directly.13
/// and will get 14
/// "A Graphics object cannot be created from an image that " 15
/// has an indexed pixel format." exception.16
/// So we have to make a temporary bitmap, and manually draw 17
/// indexed pixel format to general bitmap. 18
///</remarks>19
protected void Page_Load(object sender, EventArgs e) {20
// Bitmap uses System.Drawing namespace.21
Bitmap bmp = new Bitmap(Server.MapPath("lena.bmp"));22

23
// Size the _bmp to original bitmap's dimension.24
Bitmap _bmp = new Bitmap(bmp.Width, bmp.Height);25

26
// Uses temp _bmp to write on canvas.27
Graphics canvas = Graphics.FromImage(_bmp);28

29
// Draw the original indexed bitmap's content to the temp _bmp.30
// Paint the entire region of original bitmap to the temp _bmp.31
// Use the rectangle type to select area of source image.32
canvas.DrawImage(bmp, new Rectangle(0, 0, _bmp.Width, _bmp.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel);33

34
// Make temp _bmp to general bmp.35
bmp = _bmp;36

37
// Make a new bitmap for Upside-down.38
Bitmap bmpUpsideDown = new Bitmap(bmp.Width, bmp.Height);39

40
int r = 0, g = 0, b = 0; // R,G,B for Original bmp's RGB value.41
int avgY = (0 + bmp.Height) / 2; // avgY for average Y-axle value.42
int tarY = 0; // tarY for Upside-down Y-axle value.43
44
// Pixel by pixcel image processing.45
for (int x = 0; x < bmp.Width; x++) {46
for (int y = 0; y < bmp.Height; y++) {47
// Get RGB from original bitmap.48
r = bmp.GetPixel(x, y).R;49
g = bmp.GetPixel(x, y).G;50
b = bmp.GetPixel(x, y).B;51

52
// Cause (Origal y + Target y) /2 = Average y. 53
tarY = 2 * avgY - y;54
55
// Write to new Upsite-down bitmap on specified pixel and RGB.56
bmpUpsideDown.SetPixel(x, tarY-1, Color.FromArgb(r, g, b));57
}58
}59

60
// Specify HTML's content type.61
Response.Clear();62
Response.ContentType = "image/bmp";63

64
// ImageFormat uses System.Drawing.Imaging namespace.65
// Must use ImageFormat.Jpeg. If use ImageFormat.Bmp,66
// you'll get "System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+." error.67
bmpUpsideDown.Save(Response.OutputStream, ImageFormat.Jpeg);68

69
// You should always call the Dispose method to release 70
// the Graphics and related resources created by the 71
// FromImage method.72
_bmp.Dispose();73
bmp.Dispose();74
bmpUpsideDown.Dispose();75
canvas.Dispose();76
}77
</script>78

79
<html xmlns="http://www.w3.org/1999/xhtml">80
<head runat="server">81
<title>Untitled Page</title>82
</head>83
<body>84
<form id="form1" runat="server">85
<div>86
</div>87
</form>88
</body>89
</html>90

執行結果
See Also
(原創) 如何使用ANSI C讀寫24位元的BMP圖檔? (C/C++) (C) (Image Processing)
Reference
Steven A. Smith, Rob Howard, The ASP Alliance, ASP.NET 开发手札, 上奇科技出版事业处
KingLeon, Watermark Website Images At Runtime , The Code Project



浙公网安备 33010602011771号