Is there a way I can write the output of WPF say a canvas to a image file, jpg or the like.
I want to use WPF to create a background for me as I want to use the
BitmapEffects for a rectangle and also radius the corners.
I want to use the bitmap in a webpage.
Is this possible?
Malcolm
I have a blog post all about this here. Here's the code from the article:
Rect rect = new Rect(canvas.RenderSize);
RenderTargetBitmap rtb = new RenderTargetBitmap((int)rect.Right,
(int)rect.Bottom, 96d, 96d, System.Windows.Media.PixelFormats.Default);
rtb.Render(canvas);
//encode as PNG
BitmapEncoder pngEncoder = new PngBitmapEncoder();
pngEncoder.Frames.Add(BitmapFrame.Create(rtb));
//save to memory stream
System.IO.MemoryStream ms = new System.IO.MemoryStream();
pngEncoder.Save(ms);
ms.Close();
System.IO.File.WriteAllBytes("logo.png", ms.ToArray());
Console.WriteLine("Done");
Related
I have D3DImage _di that use to draw a background of Wpf Border in the form of a Brush.
The Image is rendered fine but i want to save the Brush to png file on disk even if the Brush is not showed on the View.
I tried as below to save it to disk but all i got is black image:
_receivedBrush =(Brush)new ImageBrush((ImageSource)_di)
RenderTargetBitmap bmpCopied = new RenderTargetBitmap(350, 174, 96, 96, PixelFormats.Default);
DrawingVisual dv = new DrawingVisual();
using (DrawingContext dc = dv.RenderOpen())
{
dc.DrawRectangle(_receivedBrush, null, new Rect(new Point(), new Size(350, 174)));
}
bmpCopied.Render(dv);
MemoryStream mse = new MemoryStream();
BmpBitmapEncoder mem = new BmpBitmapEncoder();
mem.Frames.Add(BitmapFrame.Create(bmpCopied));
mem.Save(mse);
File.WriteAllBytes(#"g:\brush.png", mse.ToArray());
mse.Close();
Thanks in advance,
Try changing to PngBitmapEncoder or change the file extension to bmp.
I have a simple wpf application WIA.My app has an image control...
I was wondering how can I save the scanned picture on my hard disk?
Depends on the type of the Image.Source, assuming that you have a BitmapSource as in the article it should be along those lines:
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create((BitmapSource)image.Source));
using (FileStream stream = new FileStream(filePath, FileMode.Create))
encoder.Save(stream);
If it's a System.Drawing.Image object then just call Save?
Is there a way to convert System.Windows.Interop.InteropBitmap to System.Drawing.Bitmap?
Thank you
I was able to solve my problem using the code below. msg.ThumbnailSource contains System.Windows.Interop.InteropBitmap type of object
BitmapSource bmpSource = msg.ThumbnailSource as BitmapSource;
MemoryStream ms = new MemoryStream();
BitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bmpSource));
encoder.Save(ms);
ms.Seek(0, SeekOrigin.Begin);
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(ms);
You could take the pixels using CopyPixels and use the code from the Bitmap.Lock/Unlock documentation to pass the pixels to the Bitmap.
My app includes a Image control which has binding to a disk image file. I some condition, the image file need be updated. But the updating can't be done because the image file is open and can not be overwritten. What should I do?
You can try to remove the binding, so the image will not be used by your program
than overwrite the image file
and than re-add the binding
i'm not sure about this, but it's worth a try
Now my solution is:
To use a converter to convert the image path into BitmapImage.
in the converter, load the image using a FileStream and copy the data into a MemoryStream and finally close the FileStream.
BitmapImage bmp = new BitmapImage();
bmp.CacheOption = BitmapCacheOption.OnLoad;
bmp.BeginInit();
var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
var memStream = new MemoryStream();
memStream.SetLength(fileStream.Length);
fileStream.Read(memStream.GetBuffer(), 0, (int)fileStream.Length);
memStream.Flush();
fileStream.Close();
bmp.StreamSource = memStream;
bmp.EndInit();
return bmp;
This is a simple Question (lets see)
I want to bind bitmap image to Image. For doing this in cs code u must write this line.
this.leftImage.Source = new BitmapImage(new Uri(#"C:\a.bmp"));
But I want make Binding from resources. Because In release time resources became part of project.exe file and if you make binding from file(Mean set Image.source with Image file address), you must always put image file in the same address(disaster programing) :)
One option is to get it from a resx file. You can do something similar to this. Assuming Images.resx contains a Left image bitmap.
leftImage.Source = ConvertBitmapToBitmapImage(Images.Left);
...
private BitmapImage ConvertBitmapToBitmapImage(Bitmap bitmap)
{
MemoryStream memoryStream = new MemoryStream();
bitmap.Save(memoryStream, ImageFormat.Png);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = new MemoryStream(memoryStream.ToArray());
bitmapImage.EndInit();
return bitmapImage;
}
With some more work, you can do this from XAML too.