Results 1 to 1 of 1

Thread: Manual Transparent Background Double Buffer Drawing

  1. #1
    Emcrank is offline C# Forum Newbie
    .NET Framework
    .NET 4.5
    Join Date
    Feb 2017
    Posts
    1
    Rep Power
    0

    Manual Transparent Background Double Buffer Drawing

    Hi,

    I am trying to draw a series of lines to a buffer that has a transparent background which will then be drawn over another window however I cannot get it to draw.

    The aim of this is to only draw the image to memory once (if i'm not mistaken that's how it works?) but to be able to draw that over a window without re-drawing the lines avoiding the flicker effect.

    Can anyone help?

    I am currently creating a new Bitmap which can contain transparent background like so
    Code:
    ManagedBackBuffer = new Bitmap(rectangleSize.Width, rectangleSize.Height, PixelFormat.Format32bppPArgb);
    Then drawing to that Bitmap
    Code:
            if (handle == default(IntPtr))
                    return;
                
                using (var g = Graphics.FromImage(ManagedBackBuffer))
                {
                    using (var p = new Pen(Color.Red, 2))
                    {
                        g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed;
                        g.Clear(Color.Transparent);
                        int centreX = Screen.FromHandle(handle).Bounds.Width / 2;
                        int centreY = Screen.FromHandle(handle).Bounds.Height / 2;
    
    
                        g.DrawLine(p, new Point(centreX - 10, centreY), new Point(centreX - 4, centreY));
                        g.DrawLine(p, new Point(centreX + 10, centreY), new Point(centreX + 4, centreY));
                        g.DrawLine(p, new Point(centreX, centreY - 10), new Point(centreX, centreY - 4));
                        g.DrawLine(p, new Point(centreX, centreY + 10), new Point(centreX, centreY + 4));
    
    
                    }
                }
    And then trying to draw it to a window using the follow
    Code:
            
            private void RenderImageToHandle(Image image, Point position, IntPtr handle)        
            {
                using (var g = Graphics.FromHwnd(handle))
                    g.DrawImage(image, position);
            }
    
            imageThread = ThreadingManager.RunWorkAsync(() =>
            {
                  while (true && handle != default(IntPtr))
                       RenderImageToHandle(ManagedBackBuffer, imagePosition, handle);
             });
    ThreadingManager.RunWorkAsync just assigns the work to a new thread, starts it and returns it.
    As i debug I'm not getting any exceptions or anything it just steps over it as if its drawing it but I cannot see anything.
    Does anyone know why this isnt drawing on the window?

    EDIT: I created a temporary form and passed it the ManagedBackBuffer image and put it in a picturebox and it looks like the Image is blank, Does anyone know why the image is blank?

    Any help appreciated. thanks.
    Last edited by Emcrank; 02-19-2017 at 5:10 PM.

Tags for this Thread

Bookmarks

Posting Permissions

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