Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: VB.NET Transparent RenderWindow  (Read 2254 times)

0 Members and 1 Guest are viewing this topic.

Recoil

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
VB.NET Transparent RenderWindow
« on: May 24, 2015, 12:20:16 am »
I have no idea if what I am trying to do is even possible because I am just starting out using SFML with VB.NET.

I currently have a picturebox that displays an image.  I am trying to use a control, a panel in this instance with transparent backcolor, to display on top of the picturebox, with about 50% transparency so the picturebox's image shows through the panel.  The panel will have additional controls inside of it that I would like to make transparent as well, but for now I am just trying to turn the panel slightly transparent.
Here is my code so far:

Public ItemWindow As RenderWindow

Sub InitGraphics()

        ItemWindow = New RenderWindow(frmMain.pnlItem.Handle)

End Sub

Sub DrawInventory()

        ItemWindow.Clear(ToSFMLColor(frmMain.pnlItem.BackColor))

        ' add items here

        ItemWindow.Display()

End Sub

Public Function ToSFMLColor(ToConvert As System.Drawing.Color) As SFML.Graphics.Color

        Return New SFML.Graphics.Color(ToConvert.R, ToConvert.G, ToConvert.G, ToConvert.A)

End Function
 

This shows the backcolor of the parent form both controls are on...so that obviously is not it.  I have done some searches on the forum here, but there is not a lot of VB.NET that I have found, and I am probably searching for the wrong terminology.  I'm probably doing this wrong anyways.

I would appreciate a point in the right direction or a better idea of how to display a half-transparent control over an image.  Thanks.

Recoil

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Re: VB.NET Transparent RenderWindow
« Reply #1 on: May 27, 2015, 01:21:01 pm »
I have tried going about this a different way, and I am calling the following in my render loop:

Public Sub DrawTempWindow()
       
        If Not InGame Then Exit Sub
       
        Dim e As New PaintEventArgs(frmMainGame.Panel1.CreateGraphics(), frmMainGame.Panel1.Bounds)

        Dim tempImage = New Bitmap(frmMainGame.GameScreen.Width, frmMainGame.GameScreen.Height, PixelFormat.Format24bppRgb)
        Dim memoryGraphics As Graphics = Graphics.FromImage(tempImage)
        Dim screenPos As Point = frmMainGame.GameScreen.PointToScreen(New Point(0, 0))
        memoryGraphics.CopyFromScreen(screenPos.X, screenPos.Y, 0, 0, frmMainGame.GameScreen.Size)

        Dim destRect As New Rectangle(0, 0, frmMainGame.Panel1.Width, frmMainGame.Panel1.Height)
        Dim srcRect As New Rectangle(frmMainGame.GameScreen.Location.X, frmMainGame.GameScreen.Location.Y, frmMainGame.Panel1.Width, frmMainGame.Panel1.Height)

        e.Graphics.DrawImage(tempImage, destRect, srcRect, GraphicsUnit.Pixel)

        tempImage.Dispose()
        memoryGraphics.Dispose()

        Dim attributes As New ImageAttributes

        Dim matrixElements As Single()() = { _
        New Single() {1.0F, 0.0F, 0.0F, 0.0F, 0.0F}, _
        New Single() {0.0F, 1.0F, 0.0F, 0.0F, 0.0F}, _
        New Single() {0.0F, 0.0F, 1.0F, 0.0F, 0.0F}, _
        New Single() {0.0F, 0.0F, 0.0F, CSng(0.4), 0.0F}, _
        New Single() {0.0F, 0.0F, 0.0F, 0.0F, 1.0F}}

        Dim matrix As ColorMatrix
        matrix = New ColorMatrix(matrixElements)
        attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default,
        ColorAdjustType.Bitmap)

        Dim bitmap As Bitmap = New Bitmap(frmMainGame.Panel1.Width, frmMainGame.Panel1.Height, PixelFormat.Format24bppRgb)
        Dim g As Graphics = Graphics.FromImage(bitmap)
        Dim brush As Brush = New SolidBrush(frmMainGame.Panel1.BackColor)
        g.FillRectangle(brush, New RectangleF(0, 0, frmMainGame.Panel1.Width, frmMainGame.Panel1.Height))
        brush.Dispose()

        e.Graphics.DrawImage(bitmap, _
        New Rectangle(0, 0, frmMainGame.Panel1.Width, frmMainGame.Panel1.Height), _
        0, 0, frmMainGame.Panel1.Width, frmMainGame.Panel1.Height, GraphicsUnit.Pixel, attributes)

        bitmap.Dispose()
        g.Dispose()
        brush.Dispose()

    End Sub
 

This shows an almost working transparent panel.  The location of my picturebox (GameScreen) and panel1 are both (208, 0), and my panel1 is 200x200 size.  This displays offset, so it is getting the image 208 pixels to the right of my panel1.  When I set [srcRect As New Rectangle(frmMainGame.GameScreen.Location.X - 208,...] it displays what is under the form, but if I only set it halfway, it will display an offset of 104 pixels, and also show 2 sections on my panel1 where it is picking up the panel1 when it takes the initial image.

I have tried turning the visibility off, getting the image, then turning panel1 visibility back on, then drawing the image, but I get the same results.

Alternatively I have also searched on how to use panel1 as a RenderWindow, then Draw() the image the same way as I do sprites, but I can find nothing on rendering an entire image, or converting the bitmap that is drawn with e.graphics to a texture.

 

anything