使用C#HttpHandler webservice创建PNG图像
我希望能够创建一个简单的PNG图像,比如使用基于ac#web的服务生成图像的红色方块,从 HTML元素
。
一些示例HTML:
是否有人可以拼凑一个简单的(工作)C#课程来让我入门? 一旦关闭和开始,我确信我可以完成这个以实际做我想要它做的事情。
- 最终游戏是为数据驱动的网页创建简单的红色/琥珀色/绿色(RAG)嵌入状态标记,以显示性能指标等*
- 我希望它能使用PNG,因为我预计未来会使用透明度*
- ASP.NET 2.0 C#解决方案请…(我还没有生产3.5的盒子)
TIA
解
rectangle.html
https://stackoverflow.com/questions/887985/create-png-image-with-c-sharp-httphandler-webservice/rectangle.ashx
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;} } }
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(); } } } }
当然这非常粗糙。 那你就叫它:
Web服务不适合这种情况。 它以特定格式返回消息,通常是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);
我认为@Lloyd的答案是一个好的开始。
我在使用Alpha透明胶片和PNG时遇到了问题: 你能用C#做一个alpha透明的PNG吗?
还有另一种方法可以完成服务动态图像。
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();
您可以传递参数以使其更具动态性。
无法从WebService输出图像。
检查: http : //www.c-sharpcorner.com/UploadFile/gnsrinivas1511/Webservice05112009034709AM/Webservice.aspx
此外,根据您的实现方式,请注意您可能正在为DOS攻击做好准备。 生成图像并不是处理器最友好的事情。 请务必使用一些身份validation和/或缓存机制来帮助缓解这一潜在的痛点。