I try to achieve to paint a form with a gradient backcolor and overlap an image with transparency.
This is possible?
I want using a tile background image with transparent background and paint the background with a custom linear gradient.
I do it!, I want share my solution with you (It's pretty easy):
External help: Tile a Shape with an Image
Private Sub BackgroundGradient(ByRef Control As Object, _
ByVal Color1 As Drawing.Color, _
ByVal Color2 As Drawing.Color)
Dim vLinearGradient As Drawing.Drawing2D.LinearGradientBrush = _
New Drawing.Drawing2D.LinearGradientBrush(New Drawing.Point(Control.Width, Control.Height), _
New Drawing.Point(Control.Width, 0), _
Color1, _
Color2)
Dim vGraphic As Drawing.Graphics = Control.CreateGraphics
' To tile the image background - Using the same image background of the image
Dim vTexture As New Drawing.TextureBrush(Control.BackgroundImage)
vGraphic.FillRectangle(vLinearGradient, Control.DisplayRectangle)
vGraphic.FillRectangle(vTexture, Control.DisplayRectangle)
vGraphic.Dispose() : vGraphic = Nothing : vTexture.Dispose() : vTexture = Nothing
End Sub
Here's how to draw the gradient background. Tiling the image will be slow unless you use the windows API or something.
Imports System.Drawing
Public Class frmBG
Private Sub frmBG_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim g As Graphics = e.Graphics
Dim p1 As Point = Me.ClientRectangle.Location
Dim p2 As Point = New Point(Me.ClientRectangle.Right, Me.ClientRectangle.Bottom)
Using brsGradient As New System.Drawing.Drawing2D.LinearGradientBrush(p1, p2, Color.Red, Color.Blue)
g.FillRectangle(brsGradient, e.ClipRectangle)
g.DrawImage(My.Resources.demoImage, Me.ClientRectangle.Location)
End Using
End Sub
Private Sub frmBG_ResizeEnd(sender As Object, e As System.EventArgs) Handles Me.ResizeEnd
Me.Invalidate()
End Sub
End Class
Related
I am using a code that creates an image tooltip when the user hovers over the button. However, for some reason, for some of the buttons, the image gets scaled larger and doesnt completely fit in the tooltip window. I have no idea why some images are getting scaled and not others. Here is the code:
Protected Overrides Sub OnLoad(e As EventArgs)
MyBase.OnLoad(e)
ToolTip1.OwnerDraw = True
For Each ctrl As Control In Controls
If TypeOf ctrl Is Button Then
ToolTip1.SetToolTip(ctrl, " ")
End If
Next
End Sub
Private Sub ToolTip1_Popup(sender As Object, e As PopupEventArgs) Handles ToolTip1.Popup
Dim oTemplate As String = e.AssociatedControl.Name
Dim ButtonPic As Image = Image.FromFile(System.IO.Path.GetFullPath("TemplatesResources\MouseHoverPics\" & oTemplate & ".png"))
e.ToolTipSize = New Size(ButtonPic.Size.Width, ButtonPic.Size.Height)
End Sub
Private Sub ToolTip1_Draw(sender As Object, e As DrawToolTipEventArgs) Handles ToolTip1.Draw
Dim oTemplate As String = e.AssociatedControl.Name
Dim ButtonPic As Image = Image.FromFile(System.IO.Path.GetFullPath("TemplatesResources\MouseHoverPics\" & oTemplate & ".png"))
e.Graphics.Clear(SystemColors.Info)
e.Graphics.DrawImage(ButtonPic, New System.Drawing.Point(0, 0))
End Sub
The results:
Looking good at first.
Scaling begins....
Even more scaling the further right I go... could it possibly have to do with the button placement? I cant see why the code would take that into consideration
Any help would be appreciated.
That happens because when you call
e.ToolTipSize = New Size(ButtonPic.Size.Width, ButtonPic.Size.Height)
you set [size of the popup = size of the image]. But what happens if you don't have enough space between your mouse cursor and the border of the window? The popup resize itself, and when you load the image inside of it, the image scale itself to match the current width/height
e.Bounds()
So all you have to do is to check the Bounds of the popup and scale manteining the ratio before call DrawImage() method.
Here's and example
Private Sub ToolTip1_Draw(sender As Object, e As DrawToolTipEventArgs) Handles ToolTip1.Draw
Dim oTemplate As String = e.AssociatedControl.Name
Dim ButtonPic As Image = Image.FromFile(System.IO.Path.GetFullPath("TemplatesResources\MouseHoverPics\" & oTemplate & ".png"))
e.Graphics.Clear(SystemColors.Info)
If ButtonPic.Width > e.Bounds.Widht Or ButtonPic.Height > e.Bounds.Height Then
[code to rescale, make it as you prefer]
End If
e.Graphics.DrawImage(ButtonPic, New System.Drawing.Point(0, 0))
End Sub
Like the image below:
![The transparency effect i mean ][1]
http://i.stack.imgur.com/ststz.jpg
That effect in vb.You can actually see the background but its not purely transparent .Its translucent afaik.
I give you some ideas:
You can set the BackColor and TransparencyKey Color properties of your form to the same color.
Then assign the transparent image that you want, through handling the Mybase.Paint event, in this way:
Private Sub frmLogin_Paint(ByVal sender As Object,
ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
If Not Me.cObjImagen Is Nothing Then
e.Graphics.DrawImage(Me.cObjImagen, 0, 0, Me.Width, Me.Height)
End If
End Sub
Where 'cObjImagen' is a system.drawing.Image loaded from 'form_load' event, or from ' public sub new() ', for example...
If you need to move the form, this will help you to process the messages correctly:
Private Const WM_NCHITTEST As Integer = 132
Private Const HTCAPTION As Integer = 2
Protected Overloads Overrides Sub WndProc(ByRef m As Message)
If m.Msg = WM_NCHITTEST Then
m.Result = New IntPtr(HTCAPTION)
Else
MyBase.WndProc(m)
End If
End Sub
I'm using a web-browser to load up images from the web automatically in a VB.Net forms applications, however, there is a white background where the image doesn't fill the whole of the navigator object on the form.
How can I go about setting a transparent background for the web browser object in my application?
Thanks,
C.
Set the form's transparency key to white.
The color you choose as the transparency key is transparent-ed out. Anything on the entire form with that color is turned into transparent. As the browser's background is of white color, a white transparency key will make it transparent, you can use Windows Aero Glass DWM effect for a glassy transparency but it would only work on Windows Vista onwards, for previous version of Windows, you'll have to paint it manually which is a long job to do. The simplest and the most quickest thing for you is to set the Transparency Key to White :)
Me.TransparencyKey = Color.White
If you want to use Aero Glass DWM, use the code below:
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Runtime.InteropServices
Private mExtendedFrameMargins As MARGINS
Protected Overrides Sub _
OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias
'use either one
e.Graphics.SmoothingMode = SmoothingMode.HighQuality
End Sub
Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
If IsGlassEnabled() Then
'You should paint the extended frame black for proper composition, but I'm painting it white as you need it
e.Graphics.FillRectangle(Brushes.White, 0, 0, Me.ClientRectangle.Width, mExtendedFrameMargins.cyTopHeight)
End If
End Sub
Private Function IsGlassEnabled() As Boolean
If Environment.OSVersion.Version.Major < 6 Then
Return False
End If
Dim isGlassSupported As Boolean = False
DwmIsCompositionEnabled(isGlassSupported)
Return isGlassSupported
End Function
<DllImport("dwmapi.dll")> _
Private Shared Function DwmIsCompositionEnabled(<MarshalAs(UnmanagedType.Bool)> ByRef pfEnabled As Boolean) As Integer
End Function
<DllImport("dwmapi.dll")> _
Private Shared Function DwmExtendFrameIntoClientArea(ByVal hwnd As IntPtr, ByRef pMarInset As MARGINS) As Integer
End Function
<StructLayout(LayoutKind.Sequential)> _
Private Structure MARGINS
Public cxLeftWidth As Integer
Public cxRightWidth As Integer
Public cyTopHeight As Integer
Public cyBottomHeight As Integer
End Structure
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If IsGlassEnabled() Then
mExtendedFrameMargins = New MARGINS
mExtendedFrameMargins.cyTopHeight = Me.Height 'type height here, this is going to be a number (integer)
DwmExtendFrameIntoClientArea(Me.Handle, mExtendedFrameMargins)
End If
End Sub
I've used this code in an app that I'm creating
The following code does not work:
Private Sub panelButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles panelButton.Click
Dim myBrush As Brush
myBrush = New SolidBrush(Color.Yellow)
fBitmap = New Bitmap(picturePanel.Width, picturePanel.Height)
Dim gg As Graphics = Graphics.FromImage(fBitmap)
gg.Clear(Color.White)
'<<<<<my attempt<<<<<<
Dim rec As Rectangle
rec = New Rectangle(picturePanel.Location.X, picturePanel.Location.Y, picturePanel.Width, picturePanel.Height)
gg.FillRectangle(myBrush, rec)
'<<<<<<<<<<<<<<<<<<<<<
'gg.FillRectangle(myBrush, gg.ClipBounds) '<<actual answer
gg.Dispose()
picturePanel.Refresh()
End Sub
In the Panel's repaint handler I've got this:
Private Sub picturePanel_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles picturePanel.Paint
If fBitmap IsNot Nothing Then
e.Graphics.DrawImage(fBitmap, 0, 0)
End If
End Sub
I've included the recommended code (marked as actual answer) but why doesn't the section marked my attempt turn the panel yellow? - can it be tweaked so that it turns the panel yellow?
rec = New Rectangle(picturePanel.Location.X, picturePanel.Location.Y, _
picturePanel.Width, picturePanel.Height)
That's the wrong rectangle. It is relative from the panel's Parent instead of the panel itself. At best you'll see the rectangle at the far right-bottom corner. Or not at all if it is off the bitmap. Draw relative to the bitmap's upper left corner instead. Fix:
rec = New Rectangle(0, 0, fBitmap.Width, fBitmap.Height)
Do note that you'll no longer see any white since you completely overdrew that. It isn't clear what you meant to do. Perhaps more illustrative is to give it a yellow border:
rec = New Rectangle(0, 0, fBitmap.Width-1, fBitmap.Height-1)
gg.DrawRectangle(Pens.Yellow, rec)
Do favor the Using statement instead of the explicit Dispose() call. Also use it on the brush, it should be disposed as well.
I'm converting a VB6 application to VB.Net that draws on picture boxes. Naturally I read the fine manual and turn up this example here. I therefore produced a little project with a form containing only a picture box and tried the following:-
Private Sub Picture1_paint(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles PictureBox1.Paint
Dim mygraphics As Graphics
mygraphics = PictureBox1.CreateGraphics
Dim pen As New Drawing.Pen(System.Drawing.Color.Red, 1)
mygraphics.DrawEllipse(pen, 0, 0, 100, 100)
pen.Dispose
End Sub
just like it says. But on running the application, the box turns up blank. Searching for help turned up a suggestion here that I should use a Frame instead, but the result was the same. I have checked that I'm not drawing in the background colour, and that the function is actually invoked.
What have I overlooked?
Paint handler has invalid type for EventArgs. It should be System.Windows.Forms.PaintEventArgs
Use e.Graphics property to obtain graphics instance.
mygraphics = e.Graphics
Reference Link MSDN - Control.Paint Event
I think e is of PainEventArgs type, with already contains a graphics object in e.Graphics. Use that instead.
Public Class Form1
Private Sub PictureBox1_Paint(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
Dim pen As New Pen(Color.Red, 1)
e.Graphics.DrawEllipse(pen, 0, 0, 100, 100)
pen.Dispose()
End Sub
End Class