namespace Microsoft.Samples.WinForms.Cs.GDIPlus.GDIPImages {
using System;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Reflection;
public class ImagesSample : System.Windows.Forms.Form {
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components;
protected internal System.Windows.Forms.Button button1;
private System.Drawing.Brush backgroundBrush;
private System.Drawing.Image sample1;
private System.Drawing.Image webLogo;
private System.Drawing.Image createdImage;
private FontFamily serifFontFamily;
public ImagesSample() {
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
serifFontFamily = new FontFamily (GenericFontFamilies.Serif);
this.SetStyle(System.Windows.Forms.ControlStyles.Opaque, true);
Image backgroundImage;
//从可执行文件的资源分支中加载要用于背景的图像
backgroundImage = new Bitmap(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("colorbars.jpg"));
//现在创建要用于绘制背景的画笔
backgroundBrush = new TextureBrush(backgroundImage);
//现在加载要使用的其他图像
sample1 = new Bitmap("sample.jpg");
//从 Web 上加载图像并进行显示。如果失败,请从本地资源中加载图像
try {
WebRequest request = WebRequest.Create("http://www.microsoft.com/net/images/bnrWindowsNgws1.gif");
request.Credentials = CredentialCache.DefaultCredentials;
Stream source = request.GetResponse().GetResponseStream();
MemoryStream ms = new MemoryStream();
byte[] data = new byte[256];
int c = source.Read(data, 0, data.Length);
while (c > 0) {
ms.Write(data, 0, c);
c = source.Read(data, 0, data.Length);
}
source.Close();
ms.Position = 0;
webLogo = new Bitmap(ms);
} catch(Exception) {
// MessageBox.Show("未能从 Web 上加载图像 - 请使用默认图像/n/n " + ex.Message + " /n/n" + ex.StackTrace, "错误");
webLogo = sample1;
}
//为呈现位图的按钮启动事件处理程序
button1.Click += new System.EventHandler(DrawImage);
}
//按下按钮时激发
private void DrawImage(object sender, EventArgs e) {
Bitmap newBitmap = null;
Graphics g = null ;
try {
newBitmap = new Bitmap(800,600,PixelFormat.Format32bppArgb);
g = Graphics.FromImage(newBitmap);
g.FillRectangle(new SolidBrush(Color.White), new Rectangle(0,0,800,600));
Font textFont = new Font(serifFontFamily, 12);
RectangleF rectangle = new RectangleF(100, 100, 250, 350);
string flowedText="我去了圣詹姆斯医院,/n到那里去看望我的爱人,/n她平躺在一张白色的桌子上,/n看上去是那么甜美、那么冰冷、那么美丽,/n让她去吧,让她去吧,愿上帝保佑她,/n不论她去哪里,/n她都可以俯瞰这个广阔的世界,/n但她永远不会找到一个像我这样的好男人,/n当我死时,希望你给我穿上整齐的系带鞋,/n我想穿一件直筒外套,戴一顶宽边帽,/n在我的表链上放上一个二十美元的金币,/n这样男孩们将知道我是站着死的。";
g.FillRectangle(new SolidBrush(Color.Gainsboro), rectangle);
g.DrawString(flowedText, textFont, new SolidBrush(Color.Blue), rectangle);
Pen penExample = new Pen(Color.FromArgb(150, Color.Purple), 20);
penExample.DashStyle = DashStyle.Dash;
penExample.StartCap = LineCap.Round;
penExample.EndCap = LineCap.Round;
g.DrawCurve(penExample, new Point[] {
new Point(200, 140),
new Point(700, 240),
new Point(500, 340),
new Point(140, 140),
new Point(40, 340),
});
newBitmap.Save("TestImage.png", ImageFormat.Png) ;
MessageBox.Show("已完成 - 图像已写入 TestImage.png");
//添加要绘制和重新绘制的图像
createdImage = newBitmap;
this.Invalidate();
} finally {
//注意:必须在处置该位图
//之前处置图形对象
//当不再需要所创建的
//图形对象时要对其进行处置
if (null != g) {
g.Dispose();
}
//通常在此处处置该位图
//但因为我们打算在窗体
//上显示它,所以先不做处置
// if (null != newBitmap) {
// newBitmap.Dispose();
// }
}
}
protected override void OnPaint(PaintEventArgs e) {
Graphics g = e.Graphics;
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
//用粗画笔填充背景
//然后应用白色涂料
g.FillRectangle(backgroundBrush, ClientRectangle);
g.FillRectangle(new SolidBrush(Color.FromArgb(180, Color.White)), ClientRectangle);
//只需呈现图像
g.DrawImage(sample1, 29, 20, 283, 212);
//现在旋转该图像并显示它
g.RotateTransform(-30);
g.DrawImage(webLogo, 175, 420);
g.ResetTransform();
//最后,对所创建的图像进行绘制(如果有该图像的话)
if (createdImage != null) {
g.DrawImage(createdImage, 50, 200, 200, 200);
}
}
/// <summary>
/// 清理正在使用的所有资源。
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing) {
if (components != null) {
components.Dispose();
}
}
base.Dispose(disposing);
}
/// <summary>
/// 设计器支持所必需的方法,不要使用
/// 代码编辑器修改此方法的内容。
/// </summary>
void InitializeComponent () {
this.components = new System.ComponentModel.Container ();
this.button1 = new System.Windows.Forms.Button ();
this.Size = new System.Drawing.Size(750, 500);
this.Text = "GDI+ 画笔示例";
button1.TabIndex = 0;
button1.Text = "将位图绘制到文件";
button1.Size = new System.Drawing.Size(100,50);
button1.Location = new System.Drawing.Point(600,20);
this.Controls.Add(button1);
}
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
public static void Main() {
Application.Run(new ImagesSample());
}
}
第二篇
<%@ Page Language="vb" Debug="True" %>
<%@ import namespace="system.drawing" %>
<%@ import namespace="system.drawing.imaging" %>
<%@ import namespace="system.drawing.drawing2d" %>
<% ''''''''''''''''''''''''''''
'让Asp.Net输出图形.
dim strFilename as string '声明变量,用于图片路径
dim i as System.Drawing.Image '创建.net框架图像处理对象实例
dim imgg1 as Integer =CInt(Int((6 * Rnd()) + 1)) '生成1-6的随机数
dim oydj as string=imgg1
strFilename ="d:/o123/"+oydj+".jpg" '载入的图片路径,这里的oydj代表随机生成的变量,用于生成随机图片
i = System.Drawing.Image.FromFile(strFilename)
dim b as New system.drawing.bitmap(i.width, i.height, pixelformat.format24bpprgb)
dim g as graphics = graphics.fromimage(b)
g.clear(color.blue) '背景色为蓝色
'用实例教学,下吧学习频道[http://123.xia8.com]
dim sss=Request.ServerVariables("REMOTE_ADDR")
g.drawimage(i,New point(0,0))
'这里表示生成一个字符,9号宋体加粗白色,pointF(160,50)为在屏幕的坐标
g.drawString(sss, New font("宋体",9,fontstyle.bold),new SolidBrush(Color.White),New pointF(160,50))
g.drawString(sss, New font("宋体",9,fontstyle.bold),new SolidBrush(Color.White),New pointF(161,51))
response.contenttype="image/jpeg" '指定输出格式为图形
b.save(response.outputstream, imageformat.jpeg)
b.dispose()
%>
第三篇:
1. create a separate page which draws the image, then in other pages having 其他的控件, use
<img src="YourImagePage.aspx?id=1231">
2. you can always use Image.Save method to save a copy on the web server
make sure ASPNET account has write permission in the directory, try
<%@ Page ContentType = "image/gif"%>
<%@ Import Namespace = "System.Drawing" %>
<%@ Import Namespace = "System.Drawing.Imaging" %>
<script runat="server" language="C#">
void Page_Load (Object sender, EventArgs e)
{
Bitmap objBitmap;
Graphics objGraphics;
objBitmap = new Bitmap(200, 200);
objGraphics = Graphics.FromImage(objBitmap);
objGraphics.DrawLine(new Pen(Color.Green), 0, 0, 200, 200);
objBitmap.Save(Response.OutputStream, ImageFormat.Gif);
objBitmap.Save(Server.MapPath("x.jpg"), ImageFormat.Jpeg);
objBitmap.Dispose();
objGraphics.Dispose();
}
</script>