何将图片上下翻转?
来源:互联网 发布:淘宝客服的工作时间 编辑:程序博客网 时间:2024/05/17 18:04
这是我修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 /// 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
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
- 何将图片上下翻转?
- Android-图片处理 之 上下,左右翻转,前景图
- 【C】矩阵翻转(上下翻转、左右翻转)
- flipud--矩阵上下翻转
- 图像平移、上下翻转、左右翻转
- S3C6410 FrameBuffer编程(六) --- 利用FrameBuffer显示图片,并实现左右、上下、180度翻转
- S3C6410 FrameBuffer编程(六) --- 利用FrameBuffer显示图片,并实现左右、上下、180度翻转
- swt 绘图坐标上下翻转
- css3三维动画菜单上下翻转
- 将图片归一化以及翻转的c++小程序
- 图片翻转
- 图片翻转
- 图片翻转
- 图片翻转
- 图片翻转
- 图片翻转
- openv-remap,图像缩小,上下翻转,左右翻转,上下左右翻转
- 图片像素翻转。水平翻转,垂直翻转。
- 《Windows NT FileSystem Internals》学习笔记之DRIVER_OBJECT对象结构
- 《Windows NT FileSystem Internals》学习笔记之IO_STACK_LOCATION的学习
- Microsoft .Net Remoting系列专题之二:Marshal、Disconnect与生命周期以及跟踪服务
- Eclipse配置kjava环境
- 《Windows NT FileSystem Internals》学习笔记之IO_STACK_LOCATION的结构
- 何将图片上下翻转?
- Microsoft .Net Remoting系列专题之三:Remoting事件处理全接触
- 《Windows NT FileSystem Internals》学习笔记之Complete IRP
- 《Windows NT File System Internals》学习笔记之Windows NT系统启动顺序
- 从文件图标上判断是否感染维金病毒
- redhat linux fedora core 5 成功安装 Oracle 10g
- 《Windows NT File System Internals》学习笔记之物理内存管理简介
- Google搜索从入门到精通v4.0
- pconline 動態廣告js