如何写一段C#代码,实现下面的VB的功能。
具体来说就是在后台写段代码,将已有的图片流显示在网页上
<%@ page language="vb" contenttype="image/jpeg" %>
<%@ import namespace="system.drawing" %>
<%@ import namespace="system.drawing.imaging" %>
<%@ import namespace="system.drawing.drawing2d" %>
<%
''''清空Response
response.clear
''''建立一个120*30大小,24bit的BMP图象;
dim imgOutput as New bitmap(120, 30, pixelformat.format24bpprgb)
''''根据以上BMP建立一个新图象;
dim g as graphics = graphics.fromimage(imgOutput)
g.clear(color.Green)
g.smoothingMode = smoothingMode.antiAlias
g.drawString("看见了吗?", New font("黑体",16,fontstyle.bold),new SolidBrush(Color.White),New pointF(2,4))
g.FillRectangle(New linearGradientBrush(New point(0,0), New point(120,30), color.fromArgb(0,0,0,0),color.fromArgb(255,255,255,255)),0,0,120,30)
imgOutput.save(response.outputstream, imageformat.jpeg)
g.dispose()
imgOutput.dispose()
response.end
%>
具体来说就是在后台写段代码,将已有的图片流显示在网页上
<%@ page language="vb" contenttype="image/jpeg" %>
<%@ import namespace="system.drawing" %>
<%@ import namespace="system.drawing.imaging" %>
<%@ import namespace="system.drawing.drawing2d" %>
<%
''''清空Response
response.clear
''''建立一个120*30大小,24bit的BMP图象;
dim imgOutput as New bitmap(120, 30, pixelformat.format24bpprgb)
''''根据以上BMP建立一个新图象;
dim g as graphics = graphics.fromimage(imgOutput)
g.clear(color.Green)
g.smoothingMode = smoothingMode.antiAlias
g.drawString("看见了吗?", New font("黑体",16,fontstyle.bold),new SolidBrush(Color.White),New pointF(2,4))
g.FillRectangle(New linearGradientBrush(New point(0,0), New point(120,30), color.fromArgb(0,0,0,0),color.fromArgb(255,255,255,255)),0,0,120,30)
imgOutput.save(response.outputstream, imageformat.jpeg)
g.dispose()
imgOutput.dispose()
response.end
%>
{
Response.Clear(); Bitmap imgOutput = new Bitmap( 120, 30, PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage( imgOutput );
g.Clear( Color.Green );
g.SmoothingMode = SmoothingMode.AntiAlias;
g.DrawString( "看见了吗?", new Font( "黑体", 16, FontStyle.Bold ),new SolidBrush( Color.White ), new PointF(2,4) );
g.FillRectangle( new LinearGradientBrush(new Point(0,0),new Point(120,30),Color.FromArgb(0,0,0,0),Color.FromArgb(255,255,255,255)),0,0,120,30);
imgOutput.Save( Response.OutputStream, ImageFormat.Jpeg );
g.Dispose();
imgOutput.Dispose();
Response.End();
}
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;然后运行那个页面就行了我试过了,可以的
我这样为什么显示不出图形
private void Button2_Click(object sender, System.EventArgs e)
{
string url="http://localhost/img/post.JPG";
System.IO.Stream stream=DownloadFile(url);
Bitmap output=new Bitmap(120,30,System.Drawing.Imaging.PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(output);
g.FillRectangle(new LinearGradientBrush(new Point(0,0),new Point(120,30),Color.FromArgb(0,0,0,0),Color.FromArgb(255,255,255,255)),0,0,120,30);
try
{
output.Save(stream,System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch
{}
finally
{
g.Dispose();
output.Dispose();
}
} public Stream DownloadFile(string FileURL)
{
HttpWebRequest r=HttpWebRequest.Create(FileURL) as HttpWebRequest;
r.AllowAutoRedirect=true;
HttpWebResponse res=r.GetResponse() as HttpWebResponse;
System.IO.Stream stream=res.GetResponseStream();
return stream;
}
imgOutput.Save( Response.OutputStream, ImageFormat.Jpeg );不知道你的DownloadFile(string FileURL) 有什么用如果只想显示
把output.Save(stream,System.Drawing.Imaging.ImageFormat.Jpeg);
改成output.Save(Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg);
就好了
http://blog.csdn.net/knight94/archive/2006/03/31/645987.aspx
DownloadFile(string FileURL) 是从一个链接下载一张图片,最后以System.IO.Stream返回。
FileStream stream new FileStream( yourImageFile,
FileMode.Open,
FileAccess.Read);// Create a buffer to hold the stream bytes
byte[] bData = new byte[stream.Length];
// Read the bytes from this stream
stream.Read(bData, 0, (int)stream.Length);
stream.Close();来替换我上面给的例子中
我来从新解释一下我的要求,
我想点击一下按钮,将一个链接上的图片下载过来,然后在自己的页面显示出来。图片不想存在本地,哪位高人能用C#给我写段代码吗?