Share via

How can i resize the image and TransparentControl programatically?

Antony 0 Reputation points
2026-03-24T23:00:09.52+00:00

I have been experimenting with VB TransparentControl as per the code that Castorix31 did about 3 years ago. https://github.com/castorix/VB_TransparentControl

I decided to try to dynamically change the size of the TransparentControl that had an image in it.

The image for arguments sake was a 600x600 png with transparency.

What I found was that I could change the size of the control but the image remained the same size. I couldn't find an imagelayout option (Zoom, Stretch, etc). I was hoping to find something akin to the VB PictureBox BackgroundImageLayout

When I changed the size of the control, the control only showed the part of the image (control and image coordinates of 0,0) as if the image had been cropped.

Is there a way that a zoom or stretch method, or even scale (maybe all three) could be added to TransparentControl that can be enabled or disabled as needed?

Regards,

Antony

Developer technologies | VB
0 comments No comments

1 answer

Sort by: Most helpful
  1. Taki Ly (WICLOUD CORPORATION) 310 Reputation points Microsoft External Staff Moderator
    2026-03-25T07:24:57.9033333+00:00

    Hello @Antony ,

    After reading through the sample you linked, it looks like the current behavior happens because the image is loaded once as a fixed HBITMAP and then sent directly to the layered window:

    Dim hBitmap As IntPtr = TransparentControl1.LoadImage("Hulk.png", System.Drawing.Color.FromArgb(0))
    TransparentControl1.SetBitmap(hBitmap)
    

    So if the TransparentControl is resized later, only the control bounds change. The image itself does not seem to be recalculated or redrawn against the new client area, which is why it can appear cropped instead of resized.

    As a workaround, I tried refactoring the sample a little so the control keeps the original image in memory, adds layout-style modes, and redraws the image when the control size changes. In my test, I focused on the three behaviors you mentioned: Zoom, Stretch, and Scale.

    Public Enum TransparentImageLayout
        Normal = 0
        Stretch = 1
        Zoom = 2
        Scale = 3
    End Enum
    

    I also trigger a redraw whenever the control is resized:

    Protected Overrides Sub OnResize(e As EventArgs)
        MyBase.OnResize(e)
        RenderCurrentImage()
    End Sub
    

    For manual scaling, I tested logic like this from the form:

    Dim newWidth As Integer = CInt(m_originalControlSize.Width * value)
    Dim newHeight As Integer = CInt(m_originalControlSize.Height * value)
    TransparentControl1.Size = New Size(newWidth, newHeight)
    TransparentControl1.ScaleFactor = value
    TransparentControl1.ImageLayoutMode = TransparentControl.TransparentImageLayout.Scale
    

    This seems to behave closer to what you described. I would not say this is the definitive fix, but it may be a useful workaround if you want TransparentControl to behave more like a PictureBox with layout-style options.

    Also, I attached a few screenshots from my test project showing zoom, stretch, and scale behavior in case that helps illustrate the approach more clearly.

    Zooming

    User's image

    Stretching

    User's image

    Scaling

    User's image

    Hope this clarifies your question. If you found my answer helpful, you could follow this guide to give feedback.

    Thank you.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.