使用C#HttpHandlerwebservice创建PNG图片元素。一些示例HTML:有人可以拼凑一个简单的(有效的)C#类来帮助我入门吗?一旦它关闭并开始,我确信我可以完成它来真正做我想做的事。TIA解rectangle.htmlhttps://stackoverflow.com/questions/887985/create-png-image-with-c-sharp-httphandler-webservice/rectangle.ashxrectangle.cs使用System.Drawing;使用System.Drawing.Imaging;使用System.IO;使用System.Web;公共类ImageHandler:IHttpHandler{publicvoidProcessRequest(HttpContextcontext){intwidth=600;//int.Parse(context.Request.QueryString["width"]);int高度=400;//int.Parse(context.Request.QueryString["height"]);位图bitmap=newBitmap(width,height);图形g=Graphics.FromImage((Image)位图);g.FillRectangle(Brushes.Red,0f,0f,bitmap.Width,bitmap.Height);//用红色矩形填充整个位图MemoryStreammem=newMemoryStream();位图.Save(mem,ImageFormat.Png);byte[]buffer=mem.ToArray();context.Response.ContentType="图片/png";context.Response.BinaryWrite(缓冲区);context.Response.Flush();}publicboolIsReusable{get{returnfalse;}}}Web服务,尤其是SOAP需要类似带有调用详细信息的XML信封之类的东西,您最好使用HttpHandler。像这样:使用System.Drawing;使用System.Drawing.Imaging;使用System.IO;使用System.Web;publicclassImageHandler:IHttpHandler{publicvoidProcessRequest(HttpContextcontext){intwidth=int.Parse(context.Request.QueryString["width"]);intheight=int.Parse(context.Request.QueryString["height"]);使用(位图位图=新位图(宽度,高度)){...使用(MemoryStreammem=newMemoryStream()){位图.Save(mem,ImageFormat.Png);mem.Seek(0,SeekOrigin.Begin);context.Response.ContentType="图片/png";mem.CopyTo(context.Response.OutputStream,4096);context.Response.Flush();}}}}当然这是很粗糙的。随你便:Web服务不适合这种情况。它以特定格式返回消息,通常是SOAP,因此它不能是图像。使用常规Web表单,删除除@page指令之外的所有标签。使用BinaryWrite方法将图像数据写入响应流。示例:byte[]图像数据;使用(Bitmapimage=newBitmap(10,10)){使用(Graphicsg=Graphics.FromImage(image)){g.Clear(Color.Red);}使用(MemoryStreamm=newMemoryStream()){image.Save(m,ImageFormat.Png);imageData=m.ToArray();}}Response.ContentType="image/png";Response.BinaryWrite(imageData);我认为@Lloyd的回答是一个好的开始。我有一个关于alpha透明胶片和PNG的问题:你能在C#中制作一个alpha透明PNG吗?还有另一种方法来完成提供动态图像。namespaceMyApp{[ServiceContract]publicinterfaceIMyService{[WebGet(UriTemplate="Image.png")][OperationContract]StreamShowImage();}}实现:publicStreamShowImage(){Bitmapimage=newBitmap(@"C:Image.png");图片image2=newBitmap(125,125);使用(Graphicsgraphics=Graphics.FromImage(image2)){graphics.InterpolationMode=InterpolationMode.HighQualityBicubic;graphics.SmoothingMode=SmoothingMode.HighQuality;graphics.PixelOffsetMode=PixelOffsetMode.HighQuality;graphics.CompositingQuality=CompositingQuality.HighQuality;graphics.DrawImage(图像,0,0,125,125);}MemoryStreamimageAsMemoryStream=newMemoryStream();image2.Save(imageAsMemoryStream.MemoryStream,ImageFormat.Png)位置=0;返回imageAsMemoryStream;将该服务作为常规WCF服务启动,并在app.config(WebService=newWebServiceHost(typeof(MyService))).Open();中添加该服务;你可以通过传递参数来使它更具动态性。无法从WebService输出图像。检查:http://www.c-sharpcorner.com/UploadFile/gnsrinivas1511/Webservice05112009034709AM/Webservice.aspx此外,根据您的实施,请注意您可能正在为DOS攻击做好准备。生成图像并不是对处理器最友好的事情。一定要使用一些身份验证和/或缓存机制来帮助缓解这个潜在的痛点。以上就是C#学习教程:使用C#HttpHandlerwebservice创建PNG图片分享。如果对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。涉及侵权,请点击维权联系管理员删除。如需转载请注明出处:
