一:WCF服务端
IService1.cs中:
public interface IService1 { [OperationContract] [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)] string Article_Pic_Add_Test(TransPicType picType); } /// <summary> /// 图片传输用到的复合类型 /// </summary> [DataContract] public class TransPicType { byte[] byteValue = null; string stringType = ""; [DataMember] public byte[] ByteValue { get; set; } [DataMember] public string StringValue { get; set; } }
Service1.svc.cs中:
public string Article_Pic_Add_Test(TransPicType picType) { string app_path = AppDomain.CurrentDomain.BaseDirectory; byte[] bs = picType.ByteValue; string getStr = picType.StringValue; //string imgString = String.Join(",", Array.ConvertAll(bs, (Converter<byte, string>)Convert.ToString));//将byte[]转换为字符串 File.WriteAllBytes(app_path + "b6.png", bs);//将图片写入app_path return null; }
二:客户端调用
0、先添加服务引用。。。这个不懂的可以搜一下、微软都搞成傻瓜一键式的了···
1、Web.config中(地址和契约根据自己的情况改下就行了):
<system.serviceModel> <behaviors> <endpointBehaviors> <behavior name="webBehavior"> <webHttp/> </behavior> </endpointBehaviors> </behaviors> <bindings> <basicHttpBinding> <binding name="BasicHttpBinding_IService1" /> </basicHttpBinding> <webHttpBinding> <!--跨域--> <binding name="webBinding"> </binding> </webHttpBinding> </bindings> <client> <endpoint address="http://localhost:16625/Service1.svc" binding="webHttpBinding" bindingConfiguration="webBinding" contract="ser.IService1" name="test" behaviorConfiguration="webBehavior" /> </client> </system.serviceModel>
2、Default.aspx.cs中:
using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebApplication1 { public partial class _Default : Page { protected void Page_Load(object sender, EventArgs e) { string app_path = AppDomain.CurrentDomain.BaseDirectory; byte[] bs = this.BmpToJpegBuff(app_path + "a.jpg"); ser.Service1Client s = new ser.Service1Client(); ser.TransPicType pt = new ser.TransPicType(); pt.ByteValue = bs; pt.StringValue = "测试文字"; s.Article_Pic_Add_Test(pt); int i = 0; } public byte[] BmpToJpegBuff(string filePath) { System.Drawing.Image img = System.Drawing.Image.FromFile(filePath); ImageConverter converter = new ImageConverter(); byte[] bmpSrc = (byte[])converter.ConvertTo(img, typeof(byte[])); MemoryStream ms = new MemoryStream(bmpSrc); MemoryStream msjpg = new MemoryStream(); Bitmap myBitmap = new Bitmap(ms); myBitmap.Save(msjpg, ImageFormat.Jpeg); byte[] bjpeg = msjpg.GetBuffer(); return bjpeg; } } }
转换图片的方法用到了:blog.csdn.net/ruijc/article/details/5809139写的、辛苦、、、O(∩_∩)O
时间: 2024-11-09 16:57:25