//if no width was given, assume the default now if(photoWidth==0) { if(!heHasAccess) { photoWidth = 100; } else { photoWidth = inputImage.Width; } }
//define size for new image percentageDifference = inputImage.Width / photoWidth; photoHeight = inputImage.Height / percentageDifference;
//output new image with different size Bitmap outputBitMap = new Bitmap(inputImage,Convert.ToInt32(photoWidth),Convert.ToInt32(photoHeight)); Response.ContentType = "image/jpeg"; outputBitMap.Save(Response.OutputStream, ImageFormat.Jpeg);
} </script>
creat one image object,then draw one pic ,then Image.Save(Response.OutputStream,ImageFormat.Gif)
double dResult; Random ro = new Random(); dResult=ro.NextDouble()*100000; iNum=dResult.ToString().Substring(0,4); try { Font fontCounter = new Font("Arial", 9);
// calculate size of the string. newBitmap = new Bitmap(1,1,PixelFormat.Format32bppArgb); g = Graphics.FromImage(newBitmap); SizeF stringSize = g.MeasureString(iNum, fontCounter); int nWidth = (int)stringSize.Width; int nHeight = (int)stringSize.Height; g.Dispose(); newBitmap.Dispose();
newBitmap = new Bitmap(nWidth,nHeight,PixelFormat.Format32bppArgb); g = Graphics.FromImage(newBitmap); g.FillRectangle(new SolidBrush(Color.White), new Rectangle(0,0,nWidth,nHeight));
g.DrawString(iNum, fontCounter, new SolidBrush(Color.Black), 0, 0);
//newBitmap.Save("c:\\test.png", ImageFormat.PNG); MemoryStream tempStream = new MemoryStream(); newBitmap.Save(tempStream,ImageFormat.Jpeg);
<img src="<%#container.dataitem("img_url")%>">
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<script runat="server"> void Page_Load(Object s, EventArgs e) {
double photoWidth,photoHeight;
double percentageDifference = 0;
bool heHasAccess = false;
System.Drawing.Image inputImage;
//get information being sent
heHasAccess = true;
//get file name
string pictureFileName = Request.QueryString["f"];
if(pictureFileName == null || pictureFileName == "") {
heHasAccess = false;
}
//get width
try {
if (Request.QueryString["w"] == null) {
photoWidth = 0;
} else {
photoWidth = Int32.Parse(Request.QueryString["w"]);
}
}
catch {
photoWidth = 0;
}
//if anything went wrong, show error picture
if(!heHasAccess) {
inputImage = System.Drawing.Image.FromFile(Server.MapPath("images/pictureNoAccess.jpg"));
} else {
inputImage = System.Drawing.Image.FromFile(Server.MapPath("images/" + pictureFileName));
}
//if no width was given, assume the default now
if(photoWidth==0) {
if(!heHasAccess) {
photoWidth = 100;
} else {
photoWidth = inputImage.Width;
}
}
//define size for new image
percentageDifference = inputImage.Width / photoWidth;
photoHeight = inputImage.Height / percentageDifference;
//output new image with different size
Bitmap outputBitMap = new Bitmap(inputImage,Convert.ToInt32(photoWidth),Convert.ToInt32(photoHeight));
Response.ContentType = "image/jpeg";
outputBitMap.Save(Response.OutputStream, ImageFormat.Jpeg);
}
</script>
Image.Save(Response.OutputStream,ImageFormat.Gif)
Graphics g = null ; String iNum;
double dResult;
Random ro = new Random();
dResult=ro.NextDouble()*100000;
iNum=dResult.ToString().Substring(0,4);
try
{
Font fontCounter = new Font("Arial", 9);
// calculate size of the string.
newBitmap = new Bitmap(1,1,PixelFormat.Format32bppArgb);
g = Graphics.FromImage(newBitmap);
SizeF stringSize = g.MeasureString(iNum, fontCounter);
int nWidth = (int)stringSize.Width;
int nHeight = (int)stringSize.Height;
g.Dispose();
newBitmap.Dispose();
newBitmap = new Bitmap(nWidth,nHeight,PixelFormat.Format32bppArgb);
g = Graphics.FromImage(newBitmap);
g.FillRectangle(new SolidBrush(Color.White),
new Rectangle(0,0,nWidth,nHeight));
g.DrawString(iNum, fontCounter,
new SolidBrush(Color.Black), 0, 0);
//newBitmap.Save("c:\\test.png", ImageFormat.PNG); MemoryStream tempStream = new MemoryStream();
newBitmap.Save(tempStream,ImageFormat.Jpeg);
Response.ClearContent();
Response.ContentType = "image/Jpeg";
Response.BinaryWrite(tempStream.ToArray());
Response.End();
}
catch (Exception ei)
{
Console.WriteLine(ei.ToString());
}
finally
{
if (null != g) g.Dispose();
if (null != newBitmap) newBitmap.Dispose();
}
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;在Page_Load事件中添加:
//定义图像的宽度、高度
int width=200;
int height=200;Bitmap b=new Bitmap(width,height);
Graphics g=Graphics.FromImage(b);
g.Clear(Color.Yellow);g.SmoothingMode=SmoothingMode.AntiAlias;
g.TextRenderingHint=TextRenderingHint.AntiAlias;Pen p=new Pen(Color.Red);
g.DrawLine(p,20,100,180,180);g.DrawString("显示的数据",new Font("arial",18,FontStyle.Bold),Brushes.DarkBlue,new PointF(10,50));//设置输出图像格式
Response.ContentType="image/Gif";
b.Save(Response.OutputStream,ImageFormat.Gif);//释放资源
b.Dispose();
g.Dispose();