[英]Create PNG image with C# HttpHandler webservice
我希望能夠創建一個簡單的PNG圖像,比如使用基於ac#web的服務生成圖像的紅色方塊,從<img src="myws.ashx?x=100>
HTML元素<img src="myws.ashx?x=100>
。
一些示例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;}
}
}
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"/>
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攻擊。 生成圖像並不是處理器最友好的事情。 請務必使用一些身份驗證和/或緩存機制來幫助緩解這一潛在的痛點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.