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); } }