(原創) 如何用程序的方式载入jpg图形文件? (.NET) (GDI+) (ASP.NET) (Image Processing)
Abstract
虽然HTML本身就提供了tag可直接显示jpg图形文件,但若需对图形本身作Pixel By Pixel的影像处理,如『影像辨识』,就必须用程序的方式将jpg图形文件加载,然后才能做后续的处理,以下的程序将示范如何在.NET平台使用C#载入jpg图形文件。
Introduction
1<!-- (C) OOMusou 2006.09.29 oomusou@hotmail.com-->
2<%@ Page Language="C#" %>
3
4<%@ Import Namespace="System.Drawing" %>
5<%@ Import Namespace="System.Drawing.Imaging" %>
6<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
7
8<script runat="server">
9 /// <summary>
10 /// Demo how to read image from Jpeg format for further
11 /// image processing.
12 /// </summary>
13 protected void Page_Load(object sender, EventArgs e) {
14
15 // Bitmap uses System.Drawing namespace.
16 Bitmap jpg = new Bitmap(Server.MapPath("lena.jpg"));
17
18 // Graphics uses System.Drawing namespace.
19 Graphics canvas = Graphics.FromImage(jpg);
20
21 // Specify HTML's content type.
22 Response.Clear();
23 Response.ContentType = "image/jpeg";
24
25 // ImageFormat uses System.Drawing.Imaging namespace.
26 jpg.Save(Response.OutputStream, ImageFormat.Jpeg);
27
28 // You should always call the Dispose method to release
29 // the Graphics and related resources created by the
30 // FromImage method.
31 jpg.Dispose();
32 canvas.Dispose();
33 }
34</script>
35
36<html xmlns="http://www.w3.org/1999/xhtml">
37<head runat="server">
38 <title></title>
39</head>
40<body>
41 <form id="form1" runat="server">
42 <div>
43 </div>
44 </form>
45</body>
46</html>
47
2<%@ Page Language="C#" %>
3
4<%@ Import Namespace="System.Drawing" %>
5<%@ Import Namespace="System.Drawing.Imaging" %>
6<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
7
8<script runat="server">
9 /// <summary>
10 /// Demo how to read image from Jpeg format for further
11 /// image processing.
12 /// </summary>
13 protected void Page_Load(object sender, EventArgs e) {
14
15 // Bitmap uses System.Drawing namespace.
16 Bitmap jpg = new Bitmap(Server.MapPath("lena.jpg"));
17
18 // Graphics uses System.Drawing namespace.
19 Graphics canvas = Graphics.FromImage(jpg);
20
21 // Specify HTML's content type.
22 Response.Clear();
23 Response.ContentType = "image/jpeg";
24
25 // ImageFormat uses System.Drawing.Imaging namespace.
26 jpg.Save(Response.OutputStream, ImageFormat.Jpeg);
27
28 // You should always call the Dispose method to release
29 // the Graphics and related resources created by the
30 // FromImage method.
31 jpg.Dispose();
32 canvas.Dispose();
33 }
34</script>
35
36<html xmlns="http://www.w3.org/1999/xhtml">
37<head runat="server">
38 <title></title>
39</head>
40<body>
41 <form id="form1" runat="server">
42 <div>
43 </div>
44 </form>
45</body>
46</html>
47
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)
(原創) 如何使用C++/CLI读/写jpg檔? (初级) (C++/CLI)
Reference
Steven A. Smith, Rob Howard, The ASP Alliance, ASP.NET 开发手札, 上奇科技出版事业处