vendredi 10 avril 2015

Drawing an image to a WPF Window using HWND

I am creating individual windows, those windows then get content added by different content providers, this could be an image, video file, capture, video stream etc


The API we're using for things like captures all take a HWND and as I'd like to be able to switch between different content in the same window I wanted to experiment with drawing an image in WPF from a HWND.


Window:



<Window x:Class="ImageTestApp.ImageWindow"
xmlns="http://ift.tt/o66D3f"
xmlns:x="http://ift.tt/mPTqtT"
Title="ImageWindow" Height="480" Width="640" Loaded="Window_Loaded">
<Grid></Grid>
</Window>


Code Behind:



public partial class ImageWindow : Window
{
public ImageWindow ()
{
InitializeComponent ();
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
var handle = new WindowInteropHelper(this).Handle;
var image = new Bitmap(@"C:\Users\jamese\Pictures\3840x2160.jpg");
Render.GDIRenderToHwnd(handle, Convert.ToInt32(ActualHeight), Convert.ToInt32(ActualWidth), image);
}
}


Rendering method using HWND:



public static void GDIRenderToHwnd ( IntPtr outputWindow, int height, int width, Bitmap bitmap )
{
Rectangle r = new Rectangle
{
Height = height,
Width = width,
Size = new Size {Height = height, Width = width},
X = 0,
Y = 0
};

using (
BufferedGraphics bg =
BufferedGraphicsManager.Current.Allocate(Graphics.FromHwndInternal(outputWindow), r))
{
using (Graphics g = bg.Graphics)
{
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;

g.Clear(Color.Aqua);

g.DrawImage(bitmap, new Point(0, 0));

bg.Render();
}
}
}


Unfortunately the above code doesn't work, I get a white window so if anyone can see an issue with it I'd appreciate some tips.


Aucun commentaire:

Enregistrer un commentaire