How to load a bitmap to ImageSource real time in wpf? - wpf

I know how to convert bitmap into BitmapImage from Load a WPF BitmapImage from a System.Drawing.Bitmap
But when my bitmap is changing, and I wanna the source of image, i.e. the BitmapImage will change with the bitmap in real time, how can I do it?
My code for generating bitmap is like this:
Rectangle rect = new Rectangle(0, 0, 300, 300);
bitmap = new Bitmap(rect.Width, rect.Height);
Graphics g = Graphics.FromImage(bitmap);
DoWithGraphics(g, rect, pictureelements);
DoWithGraphics is a series of g.FillRectangle
Thanks!

It shouldn't be a problem to display the bitmap in an Image control and update the Source property whenever the bitmap changes.

Related

Binding Bitmapimge to Image in Wpf?

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.

How to get the height of an Image in Silverlight?

I have this code in Silverlight:
Image image = new Image();
BitmapImage bitmapImage= TheDatasourceManager.GetBitmapImage("blackPencil");
image.Source = bitmapImage;
image.Stretch = Stretch.None;
image.HorizontalAlignment = HorizontalAlignment.Left;
image.VerticalAlignment = VerticalAlignment.Top;
image.Margin = new Thickness(88, 88, 0, 0);
grid.Children.Add(image);
Now I want to find out the height of the image.
in WPF I can get it with image.Source.Height but this is not available in Silverlight
bitmapImage.Height doesn't exist either
when I debug and examine the image object, I eventually get to PixelHeight which has an accurate height, but I can't seem to access it
I find image.ActualHeight but it is 0.
How can I get the height of the image?
I finally found it, it's just bitmapImage.PixelHeight. Since I'm not stretching it, seems to work fine.

Getting the height of an ImageSource in Silverlight

In my Silverlight control, I am loading my background image from a stream:
BitmapImage img = new BitmapImage();
img.SetSource(stream);
Image background = new Image();
background.Source = img;
How can I find out the height of the bitmap image that was loaded from stream? None of the usual suspects (e.g., Property, DependencyProperty) seem to be available, neither on img, nor on background.
I would try:
img.Measure();
img.DesiredSize.Height;

Write WPF output to image file

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

Resize an Uploaded Image in Silverlight 3

I'm trying to resize an image in Silverlight 3 that has been submitted by a user via the OpenFileDialog control. I can grab the contents of the file and put it into a WriteableBitmap object and then display it on the screen just fine into an Image control. The Image control will even resize it to fit the size of the image control for me which is great.
The problem is the in memory image is still the original full resolution image, I kinda need to resize it in memory because I have a bunch of expensive operations I need to perform on it on a per pixel basis. So far I have the following code...
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
btnUploadPhoto.Click += new RoutedEventHandler(UploadPhoto_Click);
}
private void UploadPhoto_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "Image files (*.png;*.jpg;*.gif;*.bmp)|*.png;*.jpg;*.gif;*.bmp";
if (dialog.ShowDialog() == true)
{
WriteableBitmap bitmap = new WriteableBitmap(500, 500);
bitmap.SetSource(dialog.File.OpenRead());
imgMainImage.Source = bitmap;
txtMessage.Text = "Image size: " + bitmap.PixelWidth + " x " + bitmap.PixelHeight;
}
}
}
Problem is the WriteableBitmap class doesn't have a Resize method on it, and setting the height and width in the constructor doesn't seem to have any effect.
What you can do is create a new Image element and set its source to a Writeable bitmap created from the stream. Don't add this Image element to the visual tree. Create another WriteableBitmap of the final size you want. Then call Render on this WriteableBitmap passing the Image element and a ScaleTransform to resize the image to the appropriate size. You can then use the second WriteableBitmap as the source for a second Image element and add that to the visual tree. You can then allow the first Image and WriteableBitmap objects to get GCed so you get the memory back.
Have you looked at the WriteableBitmapEx project? It's an open source project with a tonne of extension methods for the WriteableBitmap class. Here's how you resize:
BitmapImage image = new BitmapImage();
image.SetSource(dialog.File.OpenRead());
WriteableBitmap bitmap = new WriteableBitmap(image);
WriteableBitmap resizedBitmap = bitmap.Resize(500, 500, WriteableBitmapExtensions.Interpolation.Bilinear);
// For uploading
byte[] data = resizedBitmap.ToByteArray();
I have used FJCore with some success, it's an open source C# imaging toolkit from Occipital. Includes in-memory resizing capability.
Also check out ImageMagick.

Resources