服务器端:

1.新建一个Asp.net空网站RGImageServer。

2.新建一个WebService项目ImageService,项目新增文件ImageService.asmx,添加方法GetTile()。

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.IO;
using System.Configuration; namespace RGImageServer
{
/// <summary>
/// ImageServices 的摘要说明
/// </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 ImageServices : System.Web.Services.WebService
{ [WebMethod]
public string HelloWorld()
{
return "Hello World";
}
private byte[] Stream2Bytes(Stream theStream)
{
int num;
MemoryStream stream = new MemoryStream();
while ((num = theStream.ReadByte()) != -1)
{
stream.WriteByte((byte)num);
}
theStream.Close();
return stream.ToArray();
}
[WebMethod]
public void GetTile(string imageName)
{
string filename = ConfigurationManager.AppSettings["ImagePath"] + @"\" + imageName;
HttpContext context = this.Context;
if (File.Exists(filename))
{
try
{
FileStream theStream = File.OpenRead(filename);
context.Response.ContentType = "image/png";
byte[] buffer = Stream2Bytes(theStream);
context.Response.OutputStream.Write(buffer, 0, buffer.Length);
}
catch (Exception)
{
context.Response.StatusCode = 500;
}
}
}
}
}

3.配置WebConfig文件,发布服务。

 <?xml version="1.0" encoding="utf-8"?>

 <!--
有关如何配置 ASP.NET 应用程序的详细消息,请访问
http://go.microsoft.com/fwlink/?LinkId=169433
--> <configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<webServices>
<protocols>
<add name="HttpGet" />
<add name="HttpPost" />
<add name="HttpSoap" />
</protocols>
</webServices>
</system.web>
<appSettings>
<add key="ImagePath" value="E:\"/>
</appSettings>
</configuration>

客户端:

1.调用下载图片代码如下:

 public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Stream ContentStream;
HttpWebResponse response = null;
string ContentType;
string ContentEncoding;
int ContentLength = ;
int BytesProcessed;
private void button1_Click(object sender, EventArgs e)
{
ImageServices server = new ImageServices();
string Url = "http://localhost:6235/ImageServices.asmx/GetTile?imageName=aa.png";
string SavedFilePath = "D:\\bb.png";
// Download to file
string targetDirectory = Path.GetDirectoryName(SavedFilePath);
if (targetDirectory.Length > )
Directory.CreateDirectory(targetDirectory);
ContentStream = new FileStream(SavedFilePath, FileMode.Create);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.ContentType = "text/xml";
using (response = request.GetResponse() as HttpWebResponse)
{
// only if server responds 200 OK
if (response.StatusCode == HttpStatusCode.OK)
{
ContentType = response.ContentType;
ContentEncoding = response.ContentEncoding; // Find the data size from the headers.
string strContentLength = response.Headers["Content-Length"];
if (strContentLength != null)
{
ContentLength = int.Parse(strContentLength);
}
//缓存字节数组,大小1500byte
byte[] readBuffer = new byte[];
using (Stream responseStream = response.GetResponseStream())
{
while (true)
{
// Pass do.readBuffer to BeginRead.
int bytesRead = responseStream.Read(readBuffer, , readBuffer.Length);
if (bytesRead <= )
break;
ContentStream.Write(readBuffer, , bytesRead);
BytesProcessed += bytesRead;
}
}
ContentStream.Close();
ContentStream.Dispose();
ContentStream = null;
}
} }
}

以下是一个一般处理程序ImageHandler用于图片发布,添加ImageHandler.ashx:

 using System;
using System.Web;
using System.Configuration;
using System.IO; namespace RGImageServer
{
/// <summary>
/// ImageHandler 的摘要说明
/// </summary>
public class ImageHandler : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
//context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World"); string imageName = "aa.png";
string filename = ConfigurationManager.AppSettings["ImagePath"] + @"\" + imageName;
if (File.Exists(filename))
{
try
{
FileStream theStream = File.OpenRead(filename);
context.Response.ContentType = "image/png";
byte[] buffer = StreamToBytes(theStream);
context.Response.OutputStream.Write(buffer, , buffer.Length);
}
catch (Exception)
{ }
}
}
private byte[] StreamToBytes(Stream theStream)
{
int num;
MemoryStream stream = new MemoryStream();
while ((num = theStream.ReadByte()) != -)
{
stream.WriteByte((byte)num);
}
theStream.Close();
return stream.ToArray();
}
public bool IsReusable
{
get
{
return false;
}
}
}
}

ImageHandler

通过浏览器可以直接访问:http://localhost:6235/ImageHandler.ashx

利用WebService发布图片文件的更多相关文章

  1. 利用Selenium实现图片文件上传的两种方式介绍

    在实现UI自动化测试过程中,有一类需求是实现图片上传,这种需求根据开发的实现方式,UI的实现方式也会不同. 一.直接利用Selenium实现 这种方式是最简单的一种实现方式,但是依赖于开发的实现. 当 ...

  2. C#利用WebService接口下载文件

    WebTest.RtTfSimDataInterface test = new WebTest.RtTfSimDataInterface(); //string strBasic = test.Get ...

  3. 利用VS2008发布一个简单的webservice

    一个开发好的webservice,怎样发布出去,供其他电脑访问呢? 本文将介绍如何发布一个简单的webservice,其中的内容都是在网上查看别人文章,自己仿照着做了一遍,因此,难免会发生错误,如果发 ...

  4. winform利用ImageList控件和ListView控件组合制作图片文件浏览器

    winform利用ImageList控件和ListView控件组合制作图片文件浏览器,见图,比较简单,实现LISTVIEW显示文件夹图片功能. 1.选择文件夹功能代码: folderBrowserDi ...

  5. winform利用itextsharp.dll实现图片文件转换PDF格式文件

    1.利用itextsharp.dll实现单个图片文件转换为PDF格式文件, 可以使用以下类: void ConvertJPG2PDF(string jpgfile, string pdf) { var ...

  6. 利用COM组件IPicture读取jpg、gif、bmp图片文件数据和显示图片

    1.读取图片数据 函数原型:bool LoadImage(const char *pName, unsigned char *pBitData); 函数功能,读取pName指向的图片文件的位图数据 b ...

  7. 利用gulp把本地文件移动到指定待发布文件夹

    一.目标 把本地的文件移动到待发布的文件中,把static_grab文件中file.txt所列文件列表移动到beta对应文件夹中: 二.实现 var gulp = require('gulp'), w ...

  8. Java利用Base64编码和解码图片文件

    1.编码与解码代码如下所示: import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import jav ...

  9. ASP.NET Core应用针对静态文件请求的处理[1]: 以Web的形式发布静态文件

    虽然ASP.NET Core是一款"动态"的Web服务端框架,但是在很多情况下都需要处理针对静态文件的请求,最为常见的就是这对JavaScript脚本文件.CSS样式文件和图片文件 ...

随机推荐

  1. 《GK101任意波发生器》升级固件发布(版本:1.0.2.build124)

    一.固件说明: 硬件版本:0,logic.3 固件版本:1.0.2.build124 编译日期:2014-08-19 ====================================== 二. ...

  2. 【转】SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)

    原文地址:http://blog.csdn.net/zhshulin/article/details/37956105 使用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了, ...

  3. 用wget下载文件

    wget使用文档:https://www.gnu.org/software/wget/manual/wget.html 最开始常用的比如: wget -O  /e/movie.mp4 http://w ...

  4. Python装饰器与面向切面编程

    今天来讨论一下装饰器.装饰器是一个很著名的设计模式,经常被用于有切面需求的场景,较为经典的有插入日志.性能测试.事务处理等.装饰器是解决这类问题的绝佳设计,有了装饰器,我们就可以抽离出大量函数中与函数 ...

  5. SharedPreferences封装类SPUtils

    import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.uti ...

  6. DirectX 基础学习系列5 纹理映射

    1 纹理坐标 类似BMP图像坐标系,左上为原点 纹理坐标为了规范化,范围限定在[0,1]之间,使用纹理的时候,需要修改顶点结构 struct ColorVetex { float x, y,z; fl ...

  7. concurrency parallel 并发 并行

    Computer Systems A Programmer's Perspective Second Edition The general phenomenon of multiple flows ...

  8. Deep_learning

    https://en.wikipedia.org/wiki/Deep_learning

  9. B-Tree indexs

    mysql_High.Performance.MySQL.3rd.Edition.Mar.2012 A B-Tree index speeds up data access because the s ...

  10. [转]百度MP3音乐API接口及应用

    当你在百度去搜索一首歌时,你会发现有种更简单的方法,嘿嘿,告诉你个秘密,百度有个不公开的API http://box.zhangmen.baidu.com/x?op=12&count=1&am ...