(原創) 如何将图片以对角线做映射(Mirror)? (.NET) (ASP.NET) (C#) (GDI+) (Image Processing)
这是我修Computer Vision的作业,此源代码也示范出如何Pixel By Pixel的方式编辑图片以及如何读取indexd的bmp图片格式。
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 /// Diagonally mirrored 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 new bitmap to source 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 old bitmap's content to the new bitmap.
30 // Paint the entire region of old bitmap to the new bitmap.
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 Diagonally mirrored.
38 Bitmap bmpDiagonallyMirrored = 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 tarX = 0; // avgX for average X-axle value.
42 int tarY = 0; // tarX for Rightside-left X-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 x + y = 512 is diagonal line for lena.bmp
53 tarX = 512 - y;
54 tarY = 512 - x;
55
56 // Write to new Diagonally mirrored bitmap on specified pixel and RGB.
57 bmpDiagonallyMirrored.SetPixel(tarX - 1, tarY - 1, Color.FromArgb(r, g, b));
58 }
59 }
60
61 // Specify HTML's content type.
62 Response.Clear();
63 Response.ContentType = "image/bmp";
64
65 // ImageFormat uses System.Drawing.Imaging namespace.
66 // Must use ImageFormat.Jpeg. If use ImageFormat.Bmp,
67 // you'll get "System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+." error.
68 bmpDiagonallyMirrored.Save(Response.OutputStream, ImageFormat.Jpeg);
69
70 // You should always call the Dispose method to release
71 // the Graphics and related resources created by the
72 // FromImage method.
73 _bmp.Dispose();
74 bmp.Dispose();
75 bmpDiagonallyMirrored.Dispose();
76 canvas.Dispose();
77 }
78</script>
79
80<html xmlns="http://www.w3.org/1999/xhtml" >
81<head runat="server">
82 <title>Untitled Page</title>
83</head>
84<body>
85 <form id="form1" runat="server">
86 <div>
87
88 </div>
89 </form>
90</body>
91</html>
92
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 /// Diagonally mirrored 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 new bitmap to source 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 old bitmap's content to the new bitmap.
30 // Paint the entire region of old bitmap to the new bitmap.
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 Diagonally mirrored.
38 Bitmap bmpDiagonallyMirrored = 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 tarX = 0; // avgX for average X-axle value.
42 int tarY = 0; // tarX for Rightside-left X-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 x + y = 512 is diagonal line for lena.bmp
53 tarX = 512 - y;
54 tarY = 512 - x;
55
56 // Write to new Diagonally mirrored bitmap on specified pixel and RGB.
57 bmpDiagonallyMirrored.SetPixel(tarX - 1, tarY - 1, Color.FromArgb(r, g, b));
58 }
59 }
60
61 // Specify HTML's content type.
62 Response.Clear();
63 Response.ContentType = "image/bmp";
64
65 // ImageFormat uses System.Drawing.Imaging namespace.
66 // Must use ImageFormat.Jpeg. If use ImageFormat.Bmp,
67 // you'll get "System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+." error.
68 bmpDiagonallyMirrored.Save(Response.OutputStream, ImageFormat.Jpeg);
69
70 // You should always call the Dispose method to release
71 // the Graphics and related resources created by the
72 // FromImage method.
73 _bmp.Dispose();
74 bmp.Dispose();
75 bmpDiagonallyMirrored.Dispose();
76 canvas.Dispose();
77 }
78</script>
79
80<html xmlns="http://www.w3.org/1999/xhtml" >
81<head runat="server">
82 <title>Untitled Page</title>
83</head>
84<body>
85 <form id="form1" runat="server">
86 <div>
87
88 </div>
89 </form>
90</body>
91</html>
92