我希望能够创建一个简单的PNG图像,比如使用基于c#web的服务生成图像的红色方块,从<img src="myws.ashx?x=100>
HTML元素调用。
一些HTML示例:
<hmtl><body>
<img src="http://mysite.com/webservice/rectangle.ashx?size=100">
</body></html>
是否有人可以拼凑一个简单的(工作)C#课程来让我入门?一旦关闭和开始,我确信我可以完成这个以实际做我想做的事。
TIA
解
rectangle.html
<html>
<head></head>
<body>
<img src="rectangle.ashx" height="100" width="200">
</body>
</html>
rectangle.ashx
<%@ WebHandler Language="C#" Class="ImageHandler" %>
rectangle.cs
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Web;
public class ImageHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
int width = 600; //int.Parse(context.Request.QueryString["width"]);
int height = 400; //int.Parse(context.Request.QueryString["height"]);
Bitmap bitmap = new Bitmap(width,height);
Graphics g = Graphics.FromImage( (Image) bitmap );
g.FillRectangle( Brushes.Red, 0f, 0f, bitmap.Width, bitmap.Height ); // fill the entire bitmap with a red rectangle
MemoryStream mem = new MemoryStream();
bitmap.Save(mem,ImageFormat.Png);
byte[] buffer = mem.ToArray();
context.Response.ContentType = "image/png";
context.Response.BinaryWrite(buffer);
context.Response.Flush();
}
public bool IsReusable {
get {return false;}
}
}
答案 0 :(得分:21)
Web服务,特别是SOAP期望像XML信封这样的内容,其中包含调用的详细信息。您最好使用HttpHandler
。
这样的事情:
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Web;
public class ImageHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
int width = int.Parse(context.Request.QueryString["width"]);
int height = int.Parse(context.Request.QueryString["height"]);
using (Bitmap bitmap = new Bitmap(width,height)) {
...
using (MemoryStream mem = new MemoryStream()) {
bitmap.Save(mem,ImageFormat.Png);
mem.Seek(0,SeekOrigin.Begin);
context.Response.ContentType = "image/png";
mem.CopyTo(context.Response.OutputStream,4096);
context.Response.Flush();
}
}
}
}
这当然非常粗糙。那你就叫它:
<img src="myhandler.ashx?width=10&height=10"/>
答案 1 :(得分:3)
网络服务不适用于此。它以特定格式返回消息,通常是SOAP,因此它不能是图像。
使用常规Web表单,删除除@page
指令之外的所有标记。使用BinaryWrite
方法将图像数据写入响应流。
示例:
byte[] imageData;
using (Bitmap image = new Bitmap(10,10)) {
using (Graphics g = Graphics.FromImage(image)) {
g.Clear(Color.Red);
}
using (MemoryStream m = new MemoryStream()) {
image.Save(m, ImageFormat.Png);
imageData = m.ToArray();
}
}
Response.ContentType = "image/png";
Response.BinaryWrite(imageData);
答案 2 :(得分:2)
我认为@Lloyd的答案是一个好的开始。
我在使用Alpha透明胶片和PNG时遇到了问题:Can you make an alpha transparent PNG with C#?
答案 3 :(得分:1)
还有另一种方法可以完成动态图像的提供。
namespace MyApp
{
[ServiceContract]
public interface IMyService
{
[WebGet(UriTemplate = "Image.png")]
[OperationContract]
Stream ShowImage();
}
}
执行:
public Stream ShowImage()
{
Bitmap image = new Bitmap(@"C:\Image.png");
Image image2 = new Bitmap(125, 125);
using (Graphics graphics = Graphics.FromImage(image2))
{
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.DrawImage(image, 0, 0, 125, 125);
}
MemoryStream imageAsMemoryStream = new MemoryStream();
image2.Save(imageAsMemoryStream, ImageFormat.Png);
imageAsMemoryStream.Position = 0;
return imageAsMemoryStream;
}
以常规WCF服务启动服务,并在app.config中添加服务
(WebService = new WebServiceHost(typeof(MyService))).Open();
您可以传递参数以使其更具动态性。
答案 4 :(得分:0)
无法从WebService输出图像。
请检查:http://www.c-sharpcorner.com/UploadFile/gnsrinivas1511/Webservice05112009034709AM/Webservice.aspx
答案 5 :(得分:0)
另外,根据您的实现方式,请注意您可能会为自己设置DOS攻击。生成图像并不是处理器最友好的事情。请务必使用一些身份验证和/或缓存机制来帮助缓解这一潜在的痛点。