何将图片上下翻转?

 

这是我修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(00, _bmp.Width, _bmp.Height), 00, 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


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值