下载image或者其他文件
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); } }
如果您觉得本文对你有用,不妨帮忙点个赞,或者在评论里给我一句赞美,小小成就都是今后继续为大家编写优质文章的动力!
欢迎您持续关注我的博客:)
版权所有,欢迎保留原文链接进行转载:)