|
知名的服务器,瞻仰一下。
http://blog.csdn.net/Lost_Painti ... /02/10/5305410.aspx
WebService中实现了一个方法GetImage(),在内存中生成一张100x100的Bitmap.然后在该Bitmap上画一条线,
再将这个图片转换为byte[]传递给调用者.调用的程式将该byte[]还原为Bitmap.
以下是代码:
WebService:
C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
namespace WebService
{
/// <summary>
///MyWebService 的摘要描述
/// </summary>
[WebService(Namespace = \"http://tempuri.org/\")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允許使用 ASP.NET AJAX 從指令碼呼叫此 Web 服務,請取消註解下一行。
// [System.Web.Script.Services.ScriptService]
public class MyWebService : System.Web.Services.WebService
{
[WebMethod]
public byte[] GetImage()
{
//生成BitMap
byte[] ret = null;
Bitmap bm = new Bitmap(100, 100);
for (int i = 0; i < 100; i++)
{
bm.SetPixel(i, i, Color.Blue);
}
//从内存中读取出来存放到返回结果中
MemoryStream ms = new MemoryStream();
ImageCodecInfo[] inf = ImageCodecInfo.GetImageEncoders();
EncoderParameters eps = new EncoderParameters(1);
Encoder ec = System.Drawing.Imaging.Encoder.ColorDepth;
EncoderParameter ep = new EncoderParameter(ec, 80);
eps.Param[0] = ep;
bm.Save(ms, inf.First(info => info.MimeType == \"image/bmp\"), eps);
ret = new byte[ms.Length];
ret = ms.ToArray();
return ret;
}
}
}调用端是Winform,一个PictureBox,一个Button:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WinForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MyWebService.MyWebService ws = new MyWebService.MyWebService();
byte[] b = ws.GetImage();
Bitmap bm = new Bitmap(new MemoryStream(b));
pictureBox1.Image = bm;
}
}
} |
|