• WCF-复合类型使用;传输图片


    一: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

  • 相关阅读:
    C++ Primer中的一个sort算法源码
    字符串反转操作,网易的一道面试题
    字符编码之UCS2与Utf8
    我的Vim配置
    使用PreviousPage来获取前一页页面的元素
    ToString()用法大全
    MSSQL怎样使自动增加的id列数据归零
    确定要离开当前页面吗
    js 获取url参数
    C#判断程序是否以管理员身份运行,否则以管理员身份重新打开 转载
  • 原文地址:https://www.cnblogs.com/love-zf/p/5952074.html
Copyright © 2020-2023  润新知