public void SaveDownLoadAsPNG(Image img, string filePath)
{
try
{
WebRequest request = WebRequest.Create(filePath);
SaveFileDialog sfd = new SaveFileDialog();
string extension = System.IO.Path.GetExtension(filePath);
sfd.Filter = string.Format("*{0}| *{0}", extension);
if (sfd.ShowDialog() == true)
{
request.BeginGetResponse((responseAsyncCallBack) =>
{
this.Dispatcher.BeginInvoke(() =>
{
using (Stream openFileStream = sfd.OpenFile())
{
WebResponse response = request.EndGetResponse(responseAsyncCallBack);
Stream responseStream = response.GetResponseStream();
Byte[] bufferBytes = new Byte[responseStream.Length];
responseStream.Read(bufferBytes, 0, bufferBytes.Length); openFileStream.Write(bufferBytes, 0, bufferBytes.Length);
openFileStream.Flush();
}
}); }, null);
}
}
catch (Exception ex)
{
MessageBox.Show("Error:" + ex.ToString());
}
}

  

public void SaveAsPNG(Image img)
{
SaveFileDialog sfd = new SaveFileDialog()
{
DefaultExt = "png",
Filter = "Png files (*.png)|*.png",
FilterIndex = 1
};
if (sfd.ShowDialog() == true)
{
var bitmap = new WriteableBitmap((BitmapImage)img.Source);
EditableImage imageData = new EditableImage(bitmap.PixelWidth, bitmap.PixelHeight);
try
{
for (int y = 0; y < bitmap.PixelHeight; ++y)
{
for (int x = 0; x < bitmap.PixelWidth; ++x)
{
int pixel = bitmap.Pixels[bitmap.PixelWidth * y + x];
imageData.SetPixel(x, y,
(byte)((pixel >> 16) & 0xFF),
(byte)((pixel >> 8) & 0xFF),
(byte)(pixel & 0xFF), (byte)((pixel >> 24) & 0xFF)
);
}
}
}
catch (System.Security.SecurityException)
{
throw new Exception(Messages.Msg_CanNotPrintImage);
}
// Save it to disk
Stream pngStream = imageData.GetStream();
byte[] binaryData = new Byte[pngStream.Length];
pngStream.Read(binaryData, 0, (int)pngStream.Length);
Stream stream = sfd.OpenFile();
stream.Write(binaryData, 0, binaryData.Length);
stream.Close();
Tools.Alert(Messages.Msg_ShippingLabelImageSaveCompletely, MessageType.Information);
} }

  

下载image或者其他文件的更多相关文章

  1. ftp下载目录下所有文件及文件夹内(递归)

    ftp下载目录下所有文件及文件夹内(递归)   /// <summary> /// ftp文件上传.下载操作类 /// </summary> public class FTPH ...

  2. springMVC下载FTP上的文件

    springMVC下载FTP上的文件 今天没时间写.先上传 一个工具类 工具类 package com.utils; import java.io.File; import java.io.FileO ...

  3. Python 2.7_Second_try_爬取阳光电影网_获取电影下载地址并写入文件 20161207

    1.昨天文章http://www.cnblogs.com/Mr-Cxy/p/6139705.html 是获取电影网站主菜单 然后获取每个菜单下的电影url  2.今天是对电影url 进行再次解析获取下 ...

  4. iOS开发——网络Swift篇&NSURLSession加载数据、下载、上传文件

    NSURLSession加载数据.下载.上传文件   NSURLSession类支持三种类型的任务:加载数据.下载和上传.下面通过样例分别进行介绍.   1,使用Data Task加载数据 使用全局的 ...

  5. [转] 三种Python下载url并保存文件的代码

    原文 三种Python下载url并保存文件的代码 利用程序自己编写下载文件挺有意思的. Python中最流行的方法就是通过Http利用urllib或者urllib2模块. 当然你也可以利用ftplib ...

  6. IAR编译ZStack-CC2530为可下载运行的HEX文件的正确配置

    转自IAR编译ZStack-CC2530为可下载运行的HEX文件的正确配置 IAR编译ZStack-CC2530为可下载运行的HEX文件的正确配置:        1.正确配置输出文件格式:菜单选择P ...

  7. ASP.NET批量下载服务器端指定目录文件

    //遍历服务器指定文件夹下的所有文件 string path = "uploads/Image/"; string serverPath = Server.MapPath(path ...

  8. 【搜索引擎Jediael开发笔记2】使用HttpClient下载网页至本地文件

    本文使用HttpClient根据url进行网页下载.其中 (1)HttpClient的相关知识请参见HttpClient基础教程 (2) package org.ljh.search.download ...

  9. angular2/angular4 如何通过$http的post方法请求下载二进制的Excel文件

    时间有限,废话就不多说了,直接上干货! 下面给大家介绍一下我遇到的一个坑,如果你也遇到了,那恭喜你,你一定能找到答案:angular2/angular4 如何通过$http的post方法请求下载二进制 ...

  10. php使用curl下载指定大小的文件

    php中使用基于libcurl的curl函数,可以对目标url发起http请求并获取返回的响应内容.通常的请求方式类似如下的代码: public function callFunction($url, ...

随机推荐

  1. 查看Oracle数据库的用户名和密码

    运行 cmd 按如下输入命令 sqlplus / as sysdba ---------以sys登陆          超级用户(sysdba) alter user 用户名 account unlo ...

  2. SVO实时全局光照优化(里程碑MK2):Sparse Voxel Octree based Global Illumination (SVO GI)

    自主实现的实时渲染引擎,对标对象ue4/ce5,超越u3d/klayge.MK2版本侧重于质量与速度的均衡,以下上传示范均为实测截图,均为全分辨率(网页上显示缩小了)1080p/60fps.

  3. MongoDB副本集配置系列九:MongoDB 常见问题

    What is a namespace in MongoDB? If you remove a document, does MongoDB remove it from disk? When doe ...

  4. 浏览器 CSS Hack 收集

    所谓的Hack就是只有特定浏览器才能识别这段hack代码.Hack 不是什么好东西,除非没有办法,我们尽量还是不要用着玩意. 下面是各个浏览器的CSS Hack 列表. Firefox 浏览器 @-m ...

  5. android 6.0权限判断 音频 拍照 相册

    ------------------------------------------打开音频权限------------------------------------------------ if ...

  6. BitNami 里有好多开源软件

    https://bitnami.com/stacks BitNami 提供wordpress.joomla.drupal.bbpress等开源程序的傻瓜式安装包下载,所有的安装包内置了服务器环境,就是 ...

  7. 每日英语:Mistrust Between U.S., Malaysia Strains Probe

    Mistrust between U.S. and Malaysian air-accident investigators has hampered a multinational probe in ...

  8. Redis之高可用方案

    Redis之高可用方案   Redis以其高效的访问速度著称.但由于官方还未发布redis-cluster,而redis的replica又有诸多不便:比如一组master-slave的机器,如果之间有 ...

  9. [转] IE6中请求莫名中断

    这两天碰到的问题,IE6下某个js生成的a节点,设置了href="javascript:;",点击时触一个ajax请求,但在IE6下就是无法执行succese里的内容,所以就用se ...

  10. .net微信公众号开发——模板消息

    作者:王先荣    本文介绍微信公众号中的模板消息,包括以下内容:(1)TemplateMessage类简介:(2)设置所属行业:(3)获得模板id:(4)发送模板消息:(5)接收推送模板消息发送结果 ...