CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2012
    Posts
    2

    Unhappy 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.

    1.
    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:

    Code:
    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);
    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.

    Code:
            private ImageSource ToBitmapSource(draw.Bitmap p_bitmap)
            {
                IntPtr hBitmap = p_bitmap.GetHbitmap();
                ImageSource wpfBitmap;
                try
                {
                    wpfBitmap = Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
                }
                finally
                {
                    //p_bitmap.Dispose();
                    DeleteObject(hBitmap);
                }            
                return wpfBitmap;
            }
    Code:
     unsafe
     {
       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);
           return bitmap;
          }
     }
    Does anyone know a better solution?? How can I modify my original function to work under WPF and still get the exact same results?

    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).

    Thanks!

  2. #2
    Join Date
    Apr 2012
    Posts
    2

    Lightbulb Re: GDI+ to WPF .. Impossible??

    Ok,

    I have figured out the steps that I need to do. Can anyone show me how to do this with code, as an example?

    1. Create a new bitmap in memory
    2. Set the height and width of the bitmap to 32
    3. Color the entire bitmap red
    4. Create a second bitmap in memory
    5. Set the height and width of the second bitmap to 128
    6. Color the entire bitmap green
    7. 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.

    Thanks.

  3. #3
    Join Date
    Jan 2010
    Posts
    1,133

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured