April 2nd, 2012, 09:40 PM
GDI+ to WPF .. Impossible??
I have been searching ALL weekend for a solution. I have some GDI+ code for a video game that I need to get working in WPF. After searching through countless articles I've basically decided that it's not possible. I've posted on several other forums including MSDN forum and no one seems to know how to do it.
I know that WPF is said to be so powerfull and everything so it MUST be able to work with a simple bitmap image.
Is there a way to convert my function below to WPF, and get the exact same results? I've tried everything, including DrawingVisual but I can't get anything to even compile:
The only solution I can find it to convert back and forth between GDI+ bitmaps and WPF bitmaps, which is very messy, and way too slow for a video game.
Graphics gfx = new Graphics();
int sx = (selectedPaletteTile % PALETTE_COLUMNS) * 33;
int sy = (selectedPaletteTile / PALETTE_COLUMNS) * 33;
Rectangle src = new Rectangle(sx, sy, 32, 32); // Source
Rectangle dst = new Rectangle(0, 0, 32, 32); // Destination
gfx.DrawImage(imgPalette.Source, dst, src, draw.GraphicsUnit.Pixel);
private ImageSource ToBitmapSource(draw.Bitmap p_bitmap)
IntPtr hBitmap = p_bitmap.GetHbitmap();
wpfBitmap = Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
Does anyone know a better solution?? How can I modify my original function to work under WPF and still get the exact same results?
fixed (byte* pBits = bits)
IntPtr ptr = new IntPtr(pBits);
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(width,height,stride, System.Drawing.Imaging.PixelFormat.Format32bppPArgb,ptr);
The problem is the Graphics.DrawImage or Graphics.FromImage ... I can't get those to port over to WPF at all!
I work with Bitmap in GDI+ and have to convert that to BitmapImage to get it to work in WPF, only to find out that WPF can't work with Bitmaps (according to most of the articles I've read this weekend).
April 3rd, 2012, 01:13 PM
Re: GDI+ to WPF .. Impossible??
I have figured out the steps that I need to do. Can anyone show me how to do this with code, as an example?
- Create a new bitmap in memory
- Set the height and width of the bitmap to 32
- Color the entire bitmap red
- Create a second bitmap in memory
- Set the height and width of the second bitmap to 128
- Color the entire bitmap green
- Take the red bitmap and write it into the green bitmap at: X=15, Y=25
Basically, I am creating two bitmaps in memory. The green bitmap is a large bitmap, and the red bitmap is a small bitmap. I want to write the contents of the red bitmap onto the green bitmap at the specified location.
I can do that with GDI+ very easy with the code above. Once I can figure out how to do this, I can modify the code to work with my program.
But I can't figure out how to do this with WPF.
April 3rd, 2012, 07:39 PM
Re: GDI+ to WPF .. Impossible??
Depending on your requirements or reasons to migrate to WPF, you could simply host a WinForms control (for example, a UserConrol-derived class, or Panel-derived class, with custom GDI+ painting) in a WPF window - which will enable you to have a WPF application, but to draw your game in a Windows Forms environment.
See this article: Walkthrough: Hosting a Windows Forms Control in WPF
Click Here to Expand Forum to Full Width
This is a Codeguru.com survey!