private static byte[] GetImage(UIElement source, BitmapEncoder bitmapEncoder, double scale) { double actualHeight = source.RenderSize.Height; double actualWidth = source.RenderSize.Width; double renderHeight = actualHeight * scale; double renderWidth = actualWidth * scale; var renderTarget = new RenderTargetBitmap((int)renderWidth, (int)renderHeight, 96, 96, PixelFormats.Pbgra32); var sourceBrush = new VisualBrush(source); var drawingVisual = new DrawingVisual(); var drawingContext = drawingVisual.RenderOpen(); using (drawingContext) { drawingContext.PushTransform(new ScaleTransform(scale, scale)); drawingContext.DrawRectangle(sourceBrush, null, new Rect(new Point(0, 0), new Point(actualWidth, actualHeight))); } renderTarget.Render(drawingVisual); bitmapEncoder.Frames.Add(BitmapFrame.Create(renderTarget)); Byte[] imageArray; using (var outputStream = new MemoryStream()) { bitmapEncoder.Save(outputStream); imageArray = outputStream.ToArray(); } return imageArray; }
public Task<bool> SendMail(string sendToEmail, BitmapEncoder bitmap) { bool returnValue = true; MailMessage msg = new MailMessage("*****@*****.**", sendToEmail, "Your Microsoft Kinect Photo Booth Picture", body); msg.BodyEncoding = System.Text.Encoding.Unicode; msg.IsBodyHtml = true; ContentType ct = new ContentType(); ct.MediaType = MediaTypeNames.Image.Jpeg; ct.Name = "MSKinectPhotobooth.png"; MemoryStream stream = new MemoryStream(); bitmap.Save(stream); stream.Position = 0; Attachment data = new Attachment(stream, "MSPhotobooth.png"); msg.Attachments.Add(data); Task<bool> t = new Task<bool>(() => { try { //SmtpClient smtpClient = new SmtpClient("smtp.office365.com") SmtpClient smtpClient = new SmtpClient(_SMTPEmailServer) { UseDefaultCredentials = false, DeliveryMethod = SmtpDeliveryMethod.Network, Credentials = new NetworkCredential(_SMTPEmailUserID, _SMTPEmailPassword), }; smtpClient.Send(msg); smtpClient.Dispose(); } catch (Exception ex) { returnValue = false; } finally { msg.Dispose(); } return returnValue; }); t.Start(); return t; }
private void SaveUsingEncoder(BitmapEncoder encoder) { encoder.Frames.Add(BitmapFrame.Create(this.imageToSave)); using (var stream = File.Create(this.fileName)) { encoder.Save(stream); } }
public static void Save(this BitmapSource bitmap, string path, BitmapEncoder encoder) { using (var fs = File.OpenWrite(path)) { encoder.Frames.Add(BitmapFrame.Create(bitmap)); encoder.Save(fs); } }
public static void Save(FrameworkElement visual, Stream stream, BitmapEncoder encoder) { RenderTargetBitmap bitmap = new RenderTargetBitmap((int)visual.ActualWidth, (int)visual.ActualHeight, 96, 96, PixelFormats.Pbgra32); bitmap.Render(visual); BitmapFrame frame = BitmapFrame.Create(bitmap); encoder.Frames.Add(frame); encoder.Save(stream); }
private static void Save(string fileName, BitmapEncoder encoder, BitmapSource source) { BitmapFrame outputFrame = BitmapFrame.Create(source); encoder.Frames.Add(outputFrame); using (FileStream file = File.OpenWrite(fileName)) { encoder.Save(file); } }
public void Capture(string filePath, BitmapEncoder encoder) { var bmp = new RenderTargetBitmap((int) Width, (int) Height, 96, 96, PixelFormats.Pbgra32); bmp.Render(this); encoder.Frames.Add(BitmapFrame.Create(bmp)); using (Stream stm = File.Create(filePath)) { encoder.Save(stm); } }
public static void SaveUsingEncoder(FrameworkElement visual, string fileName, BitmapEncoder encoder) { RenderTargetBitmap bitmap = new RenderTargetBitmap((int)visual.ActualWidth, (int)visual.ActualHeight, 96, 96, PixelFormats.Pbgra32); bitmap.Render(visual); BitmapFrame frame = BitmapFrame.Create(bitmap); encoder.Frames.Add(frame); using (var stream = File.Create(fileName)) { encoder.Save(stream); } }
private static void SaveUsingEncoder(FrameworkElement visual, string fileName, BitmapEncoder encoder) { var renderTargetBitmap = new RenderTargetBitmap((int)visual.ActualWidth, (int)visual.ActualHeight, 100, 100, PixelFormats.Pbgra32); System.Console.WriteLine((int)visual.ActualWidth + "\n" + (int)visual.ActualHeight); renderTargetBitmap.Render(visual); var bitmapFrame = BitmapFrame.Create(renderTargetBitmap); encoder.Frames.Add(bitmapFrame); using (var fileStream = new FileStream(fileName, FileMode.OpenOrCreate)) { encoder.Save(fileStream); } }
/// <summary> /// Saves the given canvas using the given bitmap encoder. /// </summary> /// <param name="Canvas">The canvas.</param> /// <param name="BitmapEncoder">A bitmap encoder.</param> /// <param name="dpiX">The desired x-resolution of the saved image.</param> /// <param name="dpiY">The desired y-resolution of the saved image.</param> /// <returns>A memory stream containing the encoded JPEG image.</returns> public static MemoryStream SaveAs(this Canvas Canvas, BitmapEncoder BitmapEncoder, Double dpiX = 96d, Double dpiY = 96d) { #region Initial checks if (Canvas == null) throw new ArgumentNullException("The given canvas must not be null!"); if (BitmapEncoder == null) throw new ArgumentNullException("The bitmap encoder must not be null!"); #endregion // Save and reset current canvas transform var CanvasTransformation = Canvas.LayoutTransform; Canvas.LayoutTransform = null; // Measure and arrange the canvas var CanvasWidth = Double.IsNaN(Canvas.Width) ? Canvas.ActualWidth : Canvas.Width; var CanvasHeight = Double.IsNaN(Canvas.Height) ? Canvas.ActualHeight : Canvas.Height; var CanvasSize = new Size(CanvasWidth, CanvasHeight); Canvas.Measure(CanvasSize); Canvas.Arrange(new Rect(CanvasSize)); var _RenderTargetBitmap = new RenderTargetBitmap( (Int32) (CanvasSize.Width * dpiX / 96.0), (Int32) (CanvasSize.Height * dpiY / 96.0), dpiX, dpiY, PixelFormats.Pbgra32); _RenderTargetBitmap.Render(Canvas); // Encode bitmap var MemoryStream = new MemoryStream(); BitmapEncoder.Frames.Add(BitmapFrame.Create(_RenderTargetBitmap)); BitmapEncoder.Save(MemoryStream); // Restore previously saved layout Canvas.LayoutTransform = CanvasTransformation; return MemoryStream; }
/// <summary> /// The encode bitmap. /// </summary> /// <param name="bitmapEncoder"> /// The bitmap encoder. /// </param> /// <param name="bitmap"> /// The bitmap. /// </param> /// <returns> /// </returns> private static byte[] EncodeBitmap(BitmapEncoder bitmapEncoder, Bitmap bitmap) { BitmapSource bitmapSource = Imaging.CreateBitmapSourceFromHBitmap( bitmap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); using (var ms = new MemoryStream()) { bitmapEncoder.Frames.Add(BitmapFrame.Create(bitmapSource)); bitmapEncoder.Save(ms); return ms.ToArray(); } }
/// <summary> /// Note RenderTargetBitmap has a huge memory leak that was fixed in .NET 4.0 /// but only if you reference Winforms. /// </summary> /// <param name="copyTarget"></param> /// <param name="width"></param> /// <param name="height"></param> /// <param name="enc"></param> /// <param name="outStream"></param> /// <param name="dpi"></param> /// <returns></returns> public static Stream CopyFrameworkElementToStreamNew(FrameworkElement copyTarget, double width, double height, BitmapEncoder enc, Stream outStream, double dpi) { // Store the Frameworks current layout transform, as this will be restored later Transform storedLayoutTransform = copyTarget.LayoutTransform; Transform storedRenderTransform = copyTarget.RenderTransform; double scale = dpi / 96.0; width *= scale; height *= scale; copyTarget.RenderTransform = new ScaleTransform(scale, scale); // Set the layout transform to unity to get the nominal width and height copyTarget.LayoutTransform = new ScaleTransform(1, 1); copyTarget.UpdateLayout(); RenderTargetBitmap rtb; copyTarget.LayoutTransform = new ScaleTransform(1.0 / scale, 1.0 / scale); copyTarget.UpdateLayout(); // Render to a Bitmap Source, note that the DPI is changed for the // render target as a way of scaling the FrameworkElement rtb = new RenderTargetBitmap( (int)width, (int)height, dpi, dpi, PixelFormats.Default); rtb.Render(copyTarget); // Convert from a WinFX Bitmap Source to a Win32 Bitamp enc.Frames.Add(BitmapFrame.Create(rtb)); enc.Save(outStream); // Restore the Framework Element to it's previous state copyTarget.LayoutTransform = storedLayoutTransform; copyTarget.RenderTransform = storedRenderTransform; copyTarget.UpdateLayout(); return outStream; }
public MemoryStream RenderToImage(float dpiX, float dpiY, Rect rect, BitmapEncoder encoder) { double qualityCoefficient = Math.Floor(4096.0/(Math.Max(rect.Width, rect.Height)*dpiX/25.4)); if (Math.Abs(qualityCoefficient - 0) < GlobalData.Precision) qualityCoefficient = 4096.0/(Math.Max(rect.Width, rect.Height)*dpiX/25.4); var renderTargetBitmap = new RenderTargetBitmap((int)(rect.Width * dpiX / 25.4), (int)(rect.Height * dpiY / 25.4), dpiX * qualityCoefficient, dpiY * qualityCoefficient, PixelFormats.Pbgra32); renderTargetBitmap.Render(this); encoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap)); var stream = new MemoryStream(); encoder.Save(stream); stream.Seek(0, SeekOrigin.Begin); return stream; }
/// <summary> /// Copies a framework element to a bitmap stored in a memory stream /// </summary> /// <param name="copyTarget"></param> /// <param name="width"></param> /// <param name="height"></param> /// <param name="enc"></param> /// <param name="str"></param> /// <returns></returns> public static void CopyFrameworkElementToStream(FrameworkElement copyTarget, double width, double height, BitmapEncoder enc, Stream str) { // Store the Frameworks current layout transform, as this will be restored later Transform storedTransform = copyTarget.LayoutTransform; // Set the layout transform to unity to get the nominal width and height copyTarget.LayoutTransform = new ScaleTransform(1, 1); copyTarget.UpdateLayout(); double baseHeight = copyTarget.ActualHeight; double baseWidth = copyTarget.ActualWidth; // Now scale the layout to fit the bitmap copyTarget.LayoutTransform = new ScaleTransform(baseWidth / width, baseHeight / height); copyTarget.UpdateLayout(); // Render to a Bitmap Source, note that the DPI is changed for the // render target as a way of scaling the FrameworkElement RenderTargetBitmap rtb = new RenderTargetBitmap( (int)width, (int)height, 96d * width / baseWidth, 96d * height / baseHeight, PixelFormats.Default); rtb.Render(copyTarget); // Convert from a WPF Bitmap Source to a Win32 Bitamp enc.Frames.Add(BitmapFrame.Create(rtb)); enc.Save(str); // Restore the Framework Element to it's previous state copyTarget.LayoutTransform = storedTransform; copyTarget.UpdateLayout(); }
private string SaveResultToFile(string sourcePath, BitmapEncoder encoder) { string destinationPath = _renamer.Rename(sourcePath); if (encoder.GetType() == DefaultEncoderType) { destinationPath = Path.ChangeExtension(destinationPath, DefaultEncoderExtension); } using (var destinationStream = File.Open(destinationPath, FileMode.Create)) { // Save the final image encoder.Save(destinationStream); } return destinationPath; }
public static void Save(this BitmapSource bitmapSource, IO.Stream stream, BitmapEncoder encoder) { encoder.Frames.Add(BitmapFrame.Create(bitmapSource)); encoder.Save(stream); stream.Flush(); }
private static byte[] ConvertTo_UsingEncoder(this BitmapSource input, BitmapEncoder encoder) { if (input == null) return null; var stream = new MemoryStream(); encoder.Frames.Add(BitmapFrame.Create(input,null,null,null)); encoder.Save(stream); var serializedImage = stream.ToArray(); stream.Close(); stream.Dispose(); return serializedImage; }
private static byte[] ImageToBytes(BitmapSource img, BitmapEncoder enc) { MemoryStream memStream = new MemoryStream(); enc.Frames.Add(BitmapFrame.Create(img)); enc.Save(memStream); return memStream.GetBuffer(); }
/// <summary> /// Export the Sparrow Chart as Image /// </summary> /// <param name="fileName">Output File Name to Export the Chart as Image<</param> /// <param name="imageEncoder">Image Encoder Format for output image<</param> public void ExportAsImage(string fileName,BitmapEncoder imageEncoder) { string fileExtension = new FileInfo(fileName).Extension.ToLower(CultureInfo.InvariantCulture); RenderTargetBitmap bmpSource = new RenderTargetBitmap((int)this.ActualWidth, (int)this.ActualHeight, 96, 96, PixelFormats.Pbgra32); bmpSource.Render(this); imageEncoder.Frames.Add(BitmapFrame.Create(bmpSource)); using (Stream stream = File.Create(fileName)) { imageEncoder.Save(stream); stream.Close(); } }
public static Stream CopyFrameworkElementToStream(FrameworkElement copyTarget, double width, double height, BitmapEncoder enc, Stream outStream, double? dpi) { if (copyTarget == null) return outStream; // Store the Frameworks current layout transform, as this will be restored later Transform storedTransform = copyTarget.LayoutTransform; // Set the layout transform to unity to get the nominal width and height copyTarget.LayoutTransform = new ScaleTransform(1, 1); copyTarget.UpdateLayout(); double baseHeight = copyTarget.ActualHeight; double baseWidth = copyTarget.ActualWidth; // Now scale the layout to fit the bitmap copyTarget.LayoutTransform = new ScaleTransform(baseWidth/width, baseHeight/height); copyTarget.UpdateLayout(); // Render to a Bitmap Source, the DPI is changed for the // render target as a way of scaling the FrameworkElement var rtb = new RenderTargetBitmap( (int) width, (int) height, 96d*width/baseWidth, 96d*height/baseHeight, PixelFormats.Default); rtb.Render(copyTarget); // Convert from a WinFX Bitmap Source to a Win32 Bitamp if (dpi == null) { enc.Frames.Add(BitmapFrame.Create(rtb)); } else { // Change the DPI var brush = new ImageBrush(BitmapFrame.Create(rtb)); var rtbDpi = new RenderTargetBitmap( (int) width, // PixelWidth (int) height, // PixelHeight (double) dpi, // DpiX (double) dpi, // DpiY PixelFormats.Default); var drawVisual = new DrawingVisual(); using (DrawingContext dc = drawVisual.RenderOpen()) { dc.DrawRectangle(brush, null, new Rect(0, 0, rtbDpi.Width, rtbDpi.Height)); } rtbDpi.Render(drawVisual); enc.Frames.Add(BitmapFrame.Create(rtbDpi)); } enc.Save(outStream); // Restore the Framework Element to it's previous state copyTarget.LayoutTransform = storedTransform; copyTarget.UpdateLayout(); return outStream; }
bool SaveImage(BitmapEncoder encoder) { string time = System.DateTime.Now.ToString("hh'-'mm'-'ss", CultureInfo.CurrentUICulture.DateTimeFormat); string path = Path.Combine(KissHelper.GetPath(), "kiss-" + time + ".jpg"); try { using (FileStream fs = new FileStream(path, FileMode.Create)) encoder.Save(fs); } catch (IOException) { return false; } return true; }
bool AddVideoFrame(BitmapEncoder encoder) { if (aviStream == null) { Debug.WriteLine("aviStream is null."); return false; } using (MemoryStream s = new MemoryStream()) { encoder.Save(s); Bitmap b = (Bitmap)Image.FromStream(s); aviStream.AddFrame(b); b.Dispose(); } return true; }
private void SaveUsingEncoder(System.Windows.Controls.Image image, string fileName, BitmapEncoder encoder) { RenderTargetBitmap rtb = GetTransformedBitmap(image); encoder.Frames.Add(BitmapFrame.Create(rtb)); using (var filestream = new FileStream(fileName, FileMode.Create)) encoder.Save(filestream); }
public byte[] ImageSourceToBytes(BitmapEncoder encoder, ImageSource imageSource) { byte[] bytes = null; var bitmapSource = imageSource as BitmapSource; if (bitmapSource != null) { encoder.Frames.Add(BitmapFrame.Create(bitmapSource)); using (var stream = new MemoryStream()) { encoder.Save(stream); bytes = stream.ToArray(); } } return bytes; }