Load a WPF BitmapImage from a System.Drawing.Bitmap
Thanks to Hallgrim, here is the code I ended up with:
ScreenCapture = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
bmp.GetHbitmap(),
IntPtr.Zero,
System.Windows.Int32Rect.Empty,
BitmapSizeOptions.FromWidthAndHeight(width, height));
I also ended up binding to a BitmapSource instead of a BitmapImage as in my original question
How to Display a Bitmap in a WPF Image
I have used this snipped now to convert the Bitmap to a ImageSource:
BitmapImage BitmapToImageSource(Bitmap bitmap)
{
using (MemoryStream memory = new MemoryStream())
{
bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Bmp);
memory.Position = 0;
BitmapImage bitmapimage = new BitmapImage();
bitmapimage.BeginInit();
bitmapimage.StreamSource = memory;
bitmapimage.CacheOption = BitmapCacheOption.OnLoad;
bitmapimage.EndInit();
return bitmapimage;
}
}
Using Image control in WPF to display System.Drawing.Bitmap
You can use the Source property of the image. Try this code...
ImageSource imageSource = new BitmapImage(new Uri("C:\\FileName.gif"));
image1.Source = imageSource;
Convert System.Drawing.Bitmap to System.Windows.Media.BitmapImage for WPF
You should decode the bitmap like this:
public static BitmapSource BitmapFromBase64(string base64String)
{
var bytes = Convert.FromBase64String(base64String);
using (var stream = new MemoryStream(bytes))
{
return BitmapFrame.Create(stream,
BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
}
}
In contrast to BitmapImage
, BitmapFrame
supports the Metadata
property.
c# wpf assign bitmap
This will convert the path to ImageSource.
<Image>
<Image.Source>
<BitmapImage UriSource="{Binding ImagePath}" />
</Image.Source>
</Image>
try in this way.
public ImageSource imageSourceForImageControl
{
get
{
ImageSourceConverter c = new ImageSourceConverter();
return (ImageSource)c.ConvertFrom(yourBitmap);
}
}
https://msdn.microsoft.com/en-us/library/system.windows.media.imagesourceconverter(v=vs.110).aspx
Bitmap Drawing in WPF
If you already have existing code, I think maybe the easiest and fastest (not processing fastest though) would be to maintain your code and convert to a BitmapSource when you need to display the image:
BitmapImage bmpImage = new BitmapImage();
MemoryStream stream = new MemoryStream();
bitmap.Save(stream, ImageFormat.MemoryBmp);
bmpImage.BeginInit();
bmpImage.StreamSource = stream;
bmpImage.EndInit();
bmpImage.Freeze();
return bmpImage;
Another option, which will be more efficient but might need you to port more code, is to use the WritableBitmap. You can create the WritableBitmap in the same manner you did with your Bitmap object, and you have access to its raw backing buffer (and IntPtr) and you can manipulate the image data as you wish. This link should give you all the information you need about WritableBitmap:
WriteableBitmap
Here is an example for your case (note you need to know your dpi):
WriteableBitmap bitmap = new WriteableBitmap(pictureBox1.Width, pictureBox1.Height, dpi, dpi, PixelFormats.Bgra32, null);
if (_buffer == null)
{
_buffer = new byte[bitmap.BackBufferStride * pictureBox1.Height];
}
for (int i = 0; i < 1000; i++)
{
var x = _random.Next(bitmap.Width);
var y = _random.Next(bitmap.Height);
var red = (byte)_random.Next(byte.MaxValue);
var green = (byte)_random.Next(byte.MaxValue);
var blue = (byte)_random.Next(byte.MaxValue);
var alpha = (byte)_random.Next(byte.MaxValue);
_buffer[y * bitmap.BackBufferStride + x * 4] = blue;
_buffer[y * bitmap.BackBufferStride + x * 4 + 1] = green;
_buffer[y * bitmap.BackBufferStride + x * 4 + 2] = red;
_buffer[y * bitmap.BackBufferStride + x * 4 + 3] = alpha;
}
bitmap.WritePixels(new System.Windows.Int32Rect(0, 0, bitmap.PixelWidth, bitmap.PixelHeight),
_buffer, bitmap.PixelWidth * bitmap.Format.BitsPerPixel / 8, 0);
Related Topics
How to Make Realistic N-Body Solar System Simulation in Matter of Size and Mass
How to Read and Parse an Xml File in C#
Create Code First, Many to Many, With Additional Fields in Association Table
Validate a Username and Password Against Active Directory
Use Unity API from Another Thread or Call a Function in the Main Thread
Returning Ienumerable≪T≫ Vs. Iqueryable≪T≫
Generating All Possible Combinations
.Net String.Format() to Add Commas in Thousands Place For a Number
Calculate Difference Between Two Dates (Number of Days)
How Do Parameterized Queries Help Against SQL Injection
In .Net, Which Loop Runs Faster, 'For' or 'Foreach'
Good or Bad Practice For Dialogs in Wpf With Mvvm
How to Ignore a Property in Class If Null, Using Json.Net
Unity Scripts Edited in Visual Studio Don't Provide Autocomplete
Why Is It Important to Override Gethashcode When Equals Method Is Overridden