how to zoom in the image inside a scrollable panel in vb - vb.net

I have a scrollable panel with an image/picture box inside.
I wanted to zoom in and zoom out the image without the buttons disappearing on the lower left of the panel. by the way, the image is in it's actual size.
If I make it a scrollable panel that can be zoomed in and out, will i still be able to get the coordinates of the image by not using the coordinates of the screen but the coordinates of the actual image?
Pls help me

I'm not sure what you mean by
zoom in and zoom out the image without the buttons disappearing
But you can use this control created by Bob Powell. His site seems to be offline now but I found this code:
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
Namespace bobpowell.net
'/ <summary>
'/ ZoomPicBox does what it says on the wrapper.
'/ </summary>
'/ <remarks>
'/ PictureBox doesn't lend itself well to overriding. Why not start with something basic and do the job properly?
'/ </remarks>
Public Class ZoomPicBox
Inherits ScrollableControl
Private _image As Image
<Category("Appearance"), Description("The image to be displayed")> _
Public Property Image() As Image
Get
Return _image
End Get
Set
_image = value
UpdateScaleFactor()
Invalidate()
End Set
End Property
Private _zoom As Single = 1F
<Category("Appearance"), Description("The zoom factor. Less than 1 to reduce. More than 1 to magnify.")> _
Public Property Zoom() As Single
Get
Return _zoom
End Get
Set
If value < 0 OrElse value < 1E-05 Then
value = 1E-05F
End If
_zoom = value
UpdateScaleFactor()
Invalidate()
End Set
End Property
Private Sub UpdateScaleFactor()
If _image Is Nothing Then
Me.AutoScrollMargin = Me.Size
Else
Me.AutoScrollMinSize = New Size(CInt(Me._image.Width * _zoom + 0.5F), CInt(Me._image.Height * _zoom + 0.5F))
End If
End Sub 'UpdateScaleFactor
Private _interpolationMode As InterpolationMode = InterpolationMode.High
<Category("Appearance"), Description("The interpolation mode used to smooth the drawing")> _
Public Property InterpolationMode() As InterpolationMode
Get
Return _interpolationMode
End Get
Set
_interpolationMode = value
End Set
End Property
Protected Overrides Sub OnPaintBackground(pevent As PaintEventArgs)
End Sub 'OnPaintBackground
' do nothing.
Protected Overrides Sub OnPaint(e As PaintEventArgs)
'if no image, don't bother
If _image Is Nothing Then
MyBase.OnPaintBackground(e)
Return
End If
'Set up a zoom matrix
Dim mx As New Matrix(_zoom, 0, 0, _zoom, 0, 0)
mx.Translate(Me.AutoScrollPosition.X / _zoom, Me.AutoScrollPosition.Y / _zoom)
e.Graphics.Transform = mx
e.Graphics.InterpolationMode = _interpolationMode
e.Graphics.DrawImage(_image, New Rectangle(0, 0, Me._image.Width, Me._image.Height), 0, 0, _image.Width, _image.Height, GraphicsUnit.Pixel)
MyBase.OnPaint(e)
End Sub 'OnPaint
Public Sub New()
'Double buffer the control
Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.UserPaint Or ControlStyles.ResizeRedraw Or ControlStyles.UserPaint Or ControlStyles.DoubleBuffer, True)
Me.AutoScroll = True
End Sub 'New
End Class 'ZoomPicBox
End Namespace 'bobpowell.net

Related

Custom Toggle Control "Checked" Property Does Not Save (Or Restore) With My.Settings()

I added a custom control to my project, which is a toggle switch that inherits the functionality of a checkbox. The problem I am facing is when I try to create an Application Setting to bind the control to, it does not save (or restore, I don't really know which) the Checked: property. To be more clear, no exception is thrown. The toggle works as designed, however it does not save the state of whether it was checked or not when relaunching the program. It just defaults back to its assigned state that was set in the designer. It's just not saving the fact that it was checked or unchecked when closing or relaunching the program. The My.Settings() code is fine, I've tested it with a checkbox and it saved and restored correctly. The problem lies in the Toggle.vb Class file I'd assume. Here is the source:
Imports System.ComponentModel
Imports System.Drawing.Drawing2D
Namespace CustomControls.RJControls
Public Class RJToggleButton
Inherits CheckBox
'Fields
Private onBackColorField As Color = Color.FromArgb(128, 255, 128)
Private onToggleColorField As Color = Color.White
Private offBackColorField As Color = Color.Black
Private offToggleColorField As Color = Color.White
Private solidStyleField As Boolean = True
'Properties
<Category("RJ Code Advance")>
Public Property OnBackColor As Color
Get
Return onBackColorField
End Get
Set(ByVal value As Color)
onBackColorField = value
Me.Invalidate()
End Set
End Property
<Category("RJ Code Advance")>
Public Property OnToggleColor As Color
Get
Return onToggleColorField
End Get
Set(ByVal value As Color)
onToggleColorField = value
Me.Invalidate()
End Set
End Property
<Category("RJ Code Advance")>
Public Property OffBackColor As Color
Get
Return offBackColorField
End Get
Set(ByVal value As Color)
offBackColorField = value
Me.Invalidate()
End Set
End Property
<Category("RJ Code Advance")>
Public Property OffToggleColor As Color
Get
Return offToggleColorField
End Get
Set(ByVal value As Color)
offToggleColorField = value
Me.Invalidate()
End Set
End Property
<Browsable(False)>
Public Overrides Property Text As String
Get
Return MyBase.Text
End Get
Set(ByVal value As String)
End Set
End Property
<Category("RJ Code Advance")>
<DefaultValue(True)>
Public Property SolidStyle As Boolean
Get
Return solidStyleField
End Get
Set(ByVal value As Boolean)
solidStyleField = value
Me.Invalidate()
End Set
End Property
'Constructor
Public Sub New()
Me.MinimumSize = New Size(45, 22)
End Sub
'Methods
Private Function GetFigurePath() As GraphicsPath
Dim arcSize As Integer = Me.Height - 1
Dim leftArc As Rectangle = New Rectangle(0, 0, arcSize, arcSize)
Dim rightArc As Rectangle = New Rectangle(Me.Width - arcSize - 2, 0, arcSize, arcSize)
Dim path As GraphicsPath = New GraphicsPath()
path.StartFigure()
path.AddArc(leftArc, 90, 180)
path.AddArc(rightArc, 270, 180)
path.CloseFigure()
Return path
End Function
Protected Overrides Sub OnPaint(ByVal pevent As PaintEventArgs)
Dim toggleSize As Integer = Me.Height - 5
pevent.Graphics.SmoothingMode = SmoothingMode.AntiAlias
pevent.Graphics.Clear(Me.Parent.BackColor)
If Me.Checked Then 'ON
'Draw the control surface
If solidStyleField Then
pevent.Graphics.FillPath(New SolidBrush(onBackColorField), GetFigurePath())
Else
pevent.Graphics.DrawPath(New Pen(onBackColorField, 2), GetFigurePath())
End If
'Draw the toggle
pevent.Graphics.FillEllipse(New SolidBrush(onToggleColorField), New Rectangle(Me.Width - Me.Height + 1, 2, toggleSize, toggleSize)) 'OFF
Else
'Draw the control surface
If solidStyleField Then
pevent.Graphics.FillPath(New SolidBrush(offBackColorField), GetFigurePath())
Else
pevent.Graphics.DrawPath(New Pen(offBackColorField, 2), GetFigurePath())
End If
'Draw the toggle
pevent.Graphics.FillEllipse(New SolidBrush(offToggleColorField), New Rectangle(2, 2, toggleSize, toggleSize))
End If
End Sub
End Class
End Namespace
The binding code:
Private Sub FormMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Toggle1.Checked = My.Settings.ToggleState
End Sub
Private Sub Toggle1_CheckedChanged(sender As Object, e As EventArgs) Handles Toggle1.CheckedChanged
My.Settings.ToggleState = Toggle1.Checked
My.Settings.Save()
End Sub
My Settings:
Name: ToggleState, Type: Boolean, Scope: User, Value: True
Since I am barely a novice when it comes to coding, is there someway I can make the toggle function exactly as a checkbox, or allow it's state to be saved with My.Settings()? What am I missing to add that functionality to the toggle?
Environment: VB, .NET 6.0, Visual Basic 2022
When creating the new Application Setting, the type Boolean was assigned, and the Value assigned was True. In the form Designer, the Checked Property was also set to True. It seemed the Setting Value True or False would set the Toggle's default checked state. The Checked Value in the designer had to be set to false, in order to allow the New Setting to function properly with My.Settings().

Render multiple Image Layers to transparent PNG image

Not exactly sure how to word this question, so I will go on to explain the details, and try to ask as best I can form it.
I have a project which consists of the following components
Canvas - Inherits PictureBox control
Layers - A collection of "Layer"
Layer - Can contain a collection of Graphics as Images with information.
Each layer can be moved and the selection box for the layer is constrained to the portion of the layer which contains graphics at maximum bounds.
All of the above is working !!
What is not working, is when I want to save the combined result (including transparency, and alpha's), the Canvas control is empty. I know the images are being drawn in the box as it does not display anything until i do Canvas1.Invalidate() .
The code I have for the classes are as follows :
Canvas
Imports System.Drawing
Imports System.Drawing.Graphics
Public Class Canvas
Inherits PictureBox
Private _MoveStart As Point
Private _Layers As List(Of Layer)
Public Sub New()
Me.DoubleBuffered = True
_Layers = New List(Of Layer)
End Sub
Public ReadOnly Property Layers() As List(Of Layer)
Get
Return _Layers
End Get
End Property
Public Property SelectedLayer As Layer
Get
'Loop through all layers and return the one that is selected
For Each l As Layer In Me.Layers
If l.Selected Then Return l
Next
Return Nothing
End Get
Set(ByVal value As Layer)
'Loop through all layers and set their Selected property to True if it is the assigned layer ("value") or False if it isn't.
For Each l As Layer In Me.Layers
l.Selected = (l Is value)
Next
End Set
End Property
Private Function GetLayerFromPoint(ByVal p As Point) As Layer
' Finds the layer that contains the point p
For Each l As Layer In Me.Layers
If l.Bounds.Contains(p) Then Return l
Next
Return Nothing
End Function
Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
MyBase.OnMouseDown(e)
If e.Button = Windows.Forms.MouseButtons.Left Then
' Store the previous selected layer to refresh the image there
Dim oldSelection = Me.SelectedLayer
' Get the new selected layer
Me.SelectedLayer = Me.GetLayerFromPoint(e.Location)
'Update the picturebox
If oldSelection IsNot Nothing Then Me.InvalidateLayer(oldSelection)
Me.InvalidateLayer(Me.SelectedLayer)
Me.Update()
_MoveStart = e.Location
End If
End Sub
Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.MouseEventArgs)
MyBase.OnMouseMove(e)
If Control.MouseButtons = Windows.Forms.MouseButtons.Left Then
If Me.SelectedLayer IsNot Nothing Then
'Store the old bounds for refreshing
Dim oldBounds As Rectangle = Me.SelectedLayer.Bounds
'Move the selected layer
Me.SelectedLayer.Move(e.Location.X - _MoveStart.X, e.Location.Y - _MoveStart.Y)
_MoveStart = e.Location
'Update the picturebox
Me.InvalidateRectangle(oldBounds)
Me.InvalidateLayer(Me.SelectedLayer)
Me.Update()
End If
End If
End Sub
Private Sub InvalidateLayer(ByVal l As Layer)
If l IsNot Nothing Then
Me.InvalidateRectangle(l.Bounds)
End If
End Sub
Private Sub InvalidateRectangle(ByVal r As Rectangle)
'Inflate by 1 pixel otherwise the border isnt visible
r.Inflate(1, 1)
Me.Invalidate(r)
End Sub
Protected Overrides Sub OnPaint(ByVal pe As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(pe)
For Each l As Layer In Me.Layers
l.Draw(pe.Graphics)
Next
End Sub
End Class
Layer
Imports System.Drawing
Imports System.Drawing.Graphics
Public Class Layer
Private _Graphics As List(Of Graphic)
Private _Name As String
Private _Selected As Boolean
Public Sub New(ByVal name As String)
Me.Name = name
Me.Selected = False
Me.Graphics = New List(Of Graphic)
End Sub
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property
Public Property Selected() As Boolean
Get
Return _Selected
End Get
Set(ByVal value As Boolean)
_Selected = value
End Set
End Property
Public ReadOnly Property Bounds As Rectangle
Get
'Combine the bounds of all items
If Me.Graphics.Count > 0 Then
Dim b = Me.Graphics(0).Bounds
For i As Integer = 1 To Me.Graphics.Count - 1
b = Rectangle.Union(b, Me.Graphics(i).Bounds)
Next
Return b
End If
Return Rectangle.Empty
End Get
End Property
Public Property Graphics() As List(Of Graphic)
Get
Return _Graphics
End Get
Set(ByVal value As List(Of Graphic))
_Graphics = value
End Set
End Property
Public Sub Move(ByVal dx As Integer, ByVal dy As Integer)
'Simply move each item
For Each item As Graphic In Me.Graphics
item.Move(dx, dy)
Next
End Sub
Public Sub Draw(ByVal g As System.Drawing.Graphics)
'Draw each item
For Each item As Graphic In Me.Graphics
item.Draw(g)
Next
'Draw a selection border if selected
If Me.Selected Then
g.DrawRectangle(Pens.Red, Me.Bounds)
End If
End Sub
End Class
Graphic
Public Class Graphic
Private _Image As Image
Private _Location As Point
Private _Size As Size
Public Sub New(ByVal img As Image)
Me.Bounds = Rectangle.Empty
Me.Image = img
End Sub
Public Sub New(ByVal img As Image, ByVal location As Point)
Me.New(img)
Me.Location = location
Me.Size = img.Size
End Sub
Public Sub New(ByVal img As Image, ByVal location As Point, ByVal size As Size)
Me.New(img)
Me.Location = location
Me.Size = size
End Sub
Public Property Location() As Point
Get
Return _Location
End Get
Set(ByVal value As Point)
_Location = value
End Set
End Property
Public Property Size() As Size
Get
Return _Size
End Get
Set(ByVal value As Size)
_Size = value
End Set
End Property
Public Property Bounds() As Rectangle
Get
Return New Rectangle(Me.Location, Me.Size)
End Get
Set(ByVal value As Rectangle)
Me.Location = value.Location
Me.Size = value.Size
End Set
End Property
Public Property Image() As Image
Get
Return _Image
End Get
Set(ByVal value As Image)
_Image = value
End Set
End Property
Public Sub Move(ByVal dx As Integer, ByVal dy As Integer)
' We need to store a copy of the Location, change that, and save it back,
' because a Point is a structure and thus a value-type!!
Dim l = Me.Location
l.Offset(dx, dy)
Me.Location = l
End Sub
Public Sub Draw(ByVal g As Graphics)
If Me.Image IsNot Nothing Then
g.DrawImage(Me.Image, Me.Bounds)
End If
End Sub
End Class
Example Usage
Create a new WinForms project (Form1)
Add a Canvas object to the form (will be named Canvas1)
Add a Button object to the form (will be named Button1)
Paste the following code into the Form1 source view
Form1
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.DoubleBuffered = True
Me.Show()
Dim l As Layer
l = New Layer("Layer 1")
l.Graphics.Add(New Graphic(My.Resources.ActualSizeHS, New Point(10, 10)))
Canvas1.Layers.Add(l)
l = New Layer("Layer 2")
l.Graphics.Add(New Graphic(My.Resources.AlignObjectsRightHS, New Point(320, 240)))
l.Graphics.Add(New Graphic(My.Resources.AlignToGridHS, New Point(290, 140)))
l.Graphics.Add(New Graphic(My.Resources.AlignObjectsBottomHS, New Point(320, 130)))
Canvas1.Layers.Add(l)
l = New Layer("Layer 3")
l.Graphics.Add(New Graphic(My.Resources.AlignObjectsTopHS, New Point(520, 240)))
l.Graphics.Add(New Graphic(My.Resources.AlignTableCellMiddleRightHS, New Point(390, 240)))
l.Graphics.Add(New Graphic(My.Resources.AlignTableCellMiddleCenterHS, New Point(520, 130)))
Canvas1.Layers.Add(l)
Canvas1.Invalidate()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Canvas1.Image.Save("MyRenderedPicture.png", System.Drawing.Imaging.ImageFormat.Png)
End Sub
End Class
In the above example for Form1, replace My.Resources.* with wherever your graphics are located. This parameter is simply a System.Drawing.Image object.
The problem I am having, is when I click Button1 to save the image, the output does not contain any of the graphics that were added to the control. Please note that all of the graphics I am working with are PNG with fully transparent backgrounds, and dragging them around inside the container doesn't have the blocky effect of layering images using pictureboxes. Each image is true transparent. I wish to keep this level of transparency (and alpha blends if any exist), when i save the file -- but first ... i need to be able to save something other than a blank picturebox which clearly contains images.
Thanks in advance.
(image example of save where "shadows" are not rendering their opacity levels properly)
Now, if I do the following :
Dim x As Integer = 0
Using bmp As Bitmap = New Bitmap(Me.Width, Me.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
'Me.DrawToBitmap(bmp, New Rectangle(0, 0, bmp.Width, bmp.Height))
For Each l As Layer In Me.Layers
For Each g As Graphic In l.Graphics
g.Image.Save("layer" & x & ".png")
x = x + 1
Next
Next
bmp.MakeTransparent(Me.BackColor)
bmp.Save(FileName, Format)
bmp.Dispose()
End Using
Each layer is saved out properly -- individually. So the Graphics control is working as it should, it is when I combine them (and need to keep the position, and transparency), I think this is the routine I am looking for ---
How to merge System.Drawing.Graphics objects I am going to try and create a new Graphics object and try to "draw" onto it using the other graphics objects and their positions. Every example so far using clipping rectangles which will not do as that takes a pic of the stuff behind the Graphic which then needs to be made clear, etc etc.
You do not assign an image to the picbox/canvas so Image is Nothing. After all, you are just using it as a canvas not an image holder. Since the helpers already know where they are, you just need to create a bitmap and draw the images/layers to it from the bottom up:
Public Function GetBitmap(format As System.Drawing.Imaging.ImageFormat) As Bitmap
' ToDo: add graphics settings
Dim bmp As New Bitmap(Me.Width, Me.Height)
Using g As Graphics = Graphics.FromImage(bmp)
' ToDo: draw Canvas BG / COlor to bmp to start
' for BMP, JPG / non Transparents
For n As Integer = 0 To Layers.Count - 1
Layers(n).Draw(g)
Next
End Using
Return bmp
End Function
Then on the form:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' to do - add a literal SAve
Using bmp As Bitmap = Canvas1.GetBitmap(Imaging.ImageFormat.Png)
bmp.Save("C:\Temp\myImage.png", System.Drawing.Imaging.ImageFormat.Png)
End Using
End Sub
Are the PNGs ever being added to the Image of the Canvas? This isn't completely the same, so I apologize, but I recently made just a quick test app where I was trying to stack PNGs so thought I'd share what I did.
This loops through lstImages (which is actually a list of strings), loads the image into bmpTemp, and draws the image onto bmpBmp. That image is then used in the Image property of a PictureBox and added to the Controls collection of the form.
I just added another button to test saving and it worked fine with what I have below (after adding a name to the PictureBox).
Private Sub StackImages()
Dim bmpBmp As New Bitmap(picStack.Width, picStack.Height)
Dim graGraphic As Graphics = Graphics.FromImage(bmpBmp)
For Each i As String In Me.lstImages
Dim bmpTemp As New Bitmap(i)
graGraphic.DrawImage(bmpTemp, 0, 0)
Next
Dim picTemp As New PictureBox
picTemp.Top = picStack.Top
picTemp.Left = picStack.Left
picTemp.Width = picStack.Width
picTemp.Height = picStack.Height
picTemp.Image = bmpBmp
picTemp.Name = "NewPictureBox"
Me.Controls.Add(picTemp)
picTemp.BringToFront()
End Sub
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim picNew As PictureBox = CType(Me.Controls.Item("NewPictureBox"), PictureBox)
picNew.Image.Save("c:\temp\picTest.png")
End Sub

VB.NET Imports without project name

My form only works when I use
Imports WindowsApplication1.FrameGrabber
but not when I use
Imports FrameGrabber
I will be using the FrameGrabber in several different projects, so I would really prefer having only to say "Imports FrameGrabber".
My "FrameGrabber / CameraWindow" is defined like this:
Imports System.Collections
Imports System.ComponentModel
Imports System.Drawing
Imports System.Data
Imports System.Windows.Forms
Imports System.Threading
Namespace FrameGrabber
''' <summary>
''' Summary description for CameraWindow.
''' </summary>
Public Class CameraWindow
Inherits System.Windows.Forms.Control
Private m_camera As Camera = Nothing
Private m_autosize As Boolean = False
Private needSizeUpdate As Boolean = False
Private firstFrame As Boolean = True
' AutoSize property
<DefaultValue(False)> _
Public Overrides Property AutoSize() As Boolean
Get
Return m_autosize
End Get
Set(value As Boolean)
m_autosize = value
UpdatePosition()
End Set
End Property
' Camera property
<Browsable(False)> _
Public Property Camera() As Camera
Get
Return m_camera
End Get
Set(value As Camera)
' lock
Monitor.Enter(Me)
' detach event
If m_camera IsNot Nothing Then
RemoveHandler m_camera.NewFrame, AddressOf Me.pCameraWindow_NewFrame
End If
m_camera = value
needSizeUpdate = True
firstFrame = True
' atach event
If m_camera IsNot Nothing Then
AddHandler m_camera.NewFrame, AddressOf Me.pCameraWindow_NewFrame
End If
' unlock
Monitor.[Exit](Me)
End Set
End Property
' Constructor
Public Sub New()
InitializeComponent()
SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.DoubleBuffer Or ControlStyles.ResizeRedraw Or ControlStyles.UserPaint, True)
End Sub
#Region "Windows Form Designer generated code"
Private Sub InitializeComponent()
Me.SuspendLayout()
Me.ResumeLayout(False)
End Sub
#End Region
' Paint control
Protected Overrides Sub OnPaint(pe As PaintEventArgs)
If (needSizeUpdate) OrElse (firstFrame) Then
UpdatePosition()
needSizeUpdate = False
End If
' lock
Monitor.Enter(Me)
Dim g As Graphics = pe.Graphics
Dim rc As Rectangle = Me.ClientRectangle
If m_camera IsNot Nothing Then
Try
m_camera.Lock()
' draw frame
If m_camera.LastFrame IsNot Nothing Then
g.DrawImage(m_camera.LastFrame, rc.X + 1, rc.Y + 1, rc.Width - 2, rc.Height - 2)
firstFrame = False
Else
' Create font and brush
Dim drawFont As New Font("Arial", 12)
Dim drawBrush As New SolidBrush(Color.White)
g.DrawString("Connecting ...", drawFont, drawBrush, New System.Drawing.PointF(5, 5))
drawBrush.Dispose()
drawFont.Dispose()
End If
Catch generatedExceptionName As Exception
Finally
m_camera.Unlock()
End Try
End If
' unlock
Monitor.[Exit](Me)
MyBase.OnPaint(pe)
End Sub
Public Function getImage() As Image
If Not m_camera Is Nothing Then
If Not m_camera.LastFrame Is Nothing Then
Return m_camera.LastFrame
End If
End If
Return Nothing
End Function
' Update position and size of the control
Public Sub UpdatePosition()
' lock
Monitor.Enter(Me)
If (m_autosize) AndAlso (Me.Parent IsNot Nothing) Then
Dim rc As Rectangle = Me.Parent.ClientRectangle
Dim width As Integer = 320
Dim height As Integer = 240
If m_camera IsNot Nothing Then
m_camera.Lock()
' get frame dimension
If m_camera.LastFrame IsNot Nothing Then
width = m_camera.LastFrame.Width
height = m_camera.LastFrame.Height
End If
m_camera.Unlock()
End If
'
Me.SuspendLayout()
Me.Location = New Point((rc.Width - width - 2) \ 2, (rc.Height - height - 2) \ 2)
Me.Size = New Size(width + 2, height + 2)
Me.ResumeLayout()
End If
' unlock
Monitor.[Exit](Me)
End Sub
' On new frame ready
Private Sub pCameraWindow_NewFrame(sender As Object, e As System.EventArgs)
Invalidate()
End Sub
End Class
End Namespace
Thank you for the help!
You need to change the Root Namespace for your project or override it. When you wrap your class in a Namespace block (e.g. Namespace FrameGrabber), the given namespace is relative to the root namespace for your project. In other words, if your root namespace is WindowsApplication1, then when you say Namespace FrameGrabber, all the enclosed types will actually be in the WindowsApplication1.FrameGrabber namespace.
If you want to override the root name space for one section of code, you can use the Global keyword so that the namespace declaration is not relative, like this:
Namespace Global.FrameGrabber
' ...
End Namespace
Using the Global keyword in your namespace declaration, like that, to override the root namespace seems to be a recent addition to VB.NET, though. From what I can tell, based on the inclusion of the information about it in the MSDN article, support for that was added in Visual Studio 2012. You can also find information on it in this MSDN article:
The Global keyword can also be used in a Namespace statement. This lets you define a namespace out of the root namespace of your project. For more information, see the "Global Keyword in Namespace Statements" section in Namespaces in Visual Basic.
Another option is to remove the root namespace from your project properties and then declare the full namespace on each and every code file in your project. The setting can be found in the project settings designer screen: My Project > Application > Root namespace.
Either that or come up with a naming convention that is more conducive to VB.NET's eccentricity. For instance, if you made the root namespace for your project your company name, then MyCompany.FrameGrabber would certainly make more sense than WindowsApplication1.FrameGrabber.

Highlighting around textboxes

I am trying to draw a highlighted border around a custom textbox control so that I can reuse the highlighting feature for each new program I create. My approach so far has been to override the paint event in the control library (dll) after the custom property I have created is set. The code for the control is below.
Imports System.Windows.Forms
Imports System.ComponentModel
Imports System.Drawing
Imports System.ComponentModel.Design
<ToolboxBitmap(GetType(Button))>
Public Class Textbox_Custom
Inherits System.Windows.Forms.TextBox
Public Event OnEnterKeyPress()
Public Event MissingInfo_Change As EventHandler
Dim iMissing_Info As Boolean
Dim iCharacterInput As Cinput
Public Property CharacterInput As Cinput
'<Browsable(True), DefaultValue("AllowAll")>
Get
Return Me.iCharacterInput
End Get
Set(ByVal value As Cinput)
Me.iCharacterInput = value
End Set
End Property
Public Property Missing_Info As Boolean
'<Browsable(True), DefaultValue(True)>
Get
Return iMissing_Info
End Get
Set(value As Boolean)
iMissing_Info = value
**MyBase.Refresh()**
End Set
End Property
Protected Overrides Sub OnKeyPress(e As KeyPressEventArgs)
MyBase.OnKeyPress(e)
If Asc(e.KeyChar) = 13 Then
RaiseEvent OnEnterKeyPress()
End If
Select Case Me.iCharacterInput
Case Cinput.CharactersOnly
If IsNumeric(e.KeyChar) Then
e.Handled = True
End If
Case Cinput.NumericOnly
If Not IsNumeric(e.KeyChar) And Asc(e.KeyChar) <> 8 Then
e.Handled = True
End If
End Select
End Sub
Protected Overrides Sub OnPaint(e As PaintEventArgs)
MyBase.OnPaint(e)
**If iMissing_Info = True Then**
Dim rect As New Rectangle(New Point(0, 0), New Size(Me.Size.Width + 2, Me.Size.Height + 2))
Dim pen As New Pen(Brushes.OrangeRed, 2)
e.Graphics.DrawRectangle(pen, rect)
e.Dispose()
End If
End Sub
End Class
Public Enum Cinput
AllowAll
NumericOnly
CharactersOnly
End Enum
While debugging I have set a breakpoint in the OnPaint override (lines **), but it never hits it. I then put a breakpoint in the Set section of the Missing_Info property where I am trying to invalidate the control to redraw. It does hit the MyBase.Refresh breakpoint so I don't understand what I've missed.
I realize there have been several other posts on this topic, but from what I can tell they seem to require putting panels behind the control. I feel like I should be able to include this action in a custom control and not have to code a new highlighting section for each new project. Thanks for any help in advance.
In the end I decided to just go with changing the control background to a semi-transparent red color which should be obvious enough for what I'm doing.

vb .NET custom control inheriting from TextBox doesn't fire Paint event

I need a multiline TextBox which is always disabled, but it shouldn't paint itself in gray, but I want to keep its designer choosen color.
I previously had the same requirement with an always-black Label (no multiline) and so I inherited from Label like:
Imports System.ComponentModel
Public Class LabelDisabled
Inherits Label
Sub New()
InitializeComponent()
Enabled = False
End Sub
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
' always draw it black
e.Graphics.DrawString(Me.Text, Me.Font, Brushes.Black, 0, 0)
End Sub
End Class
That works fine. Now I want the same thing but with a multiline label, so I chose to inherit from TextBox:
Imports System.ComponentModel
Public Class CustomControl1
Inherits TextBox
Sub New()
InitializeComponent()
'Paint never fires anyway
'Enabled = False
End Sub
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
Dim brush As New SolidBrush(Me.ForeColor)
e.Graphics.DrawString(Me.Text, Me.Font, brush, 0, 0)
End Sub
End Class
Now the Paint event is never fired in the CustomControl1 - TextBox inherited - control.
Why can't I get the Paint event?
Also, if I want to make the Enabled property invisible and not-settable by the user, I do:
<Browsable(False),
DefaultValue(False)>
Public Overloads Property Enabled As Boolean
Get
Return False
End Get
Set(ByVal value As Boolean)
End Set
End Property
But this way, neither I can set the "real" Enabled property, I mean the backing field.
I've found a solution. It looks like a TextBox disables the Paint event even for subclasses. But you can force the WM_PAINT bit calling SetStyle:
Public Class DisabledTextBox
Inherits TextBox
Public Sub New()
InitializeComponent()
Enabled = False
SetStyle(ControlStyles.Selectable, False)
SetStyle(ControlStyles.UserPaint, True)
End Sub
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
Dim brush As New SolidBrush(Me.ForeColor)
e.Graphics.DrawString(Me.Text, Me.Font, brush, 0, 0)
End Sub
End Class
It works perfectly as expected :)
here is your answer:
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)
e.Graphics.FillRectangle(Brushes.LightGray, Me.DisplayRectangle)
Dim sf As New StringFormat
sf.FormatFlags = StringFormatFlags.NoWrap
sf.HotkeyPrefix = Drawing.Text.HotkeyPrefix.Show 'if Mnemonic property is set to true
sf.HotkeyPrefix = Drawing.Text.HotkeyPrefix.Hide 'or none if Mnemonic property is set to false
sf.LineAlignment = StringAlignment.Center 'horizontal alignment
sf.Alignment = StringAlignment.Center ' vertical ...
Dim rect As Rectangle = Me.DisplayRectangle ' this is your text bounds for setting your text alignement using StringFormat(sf)
e.Graphics.DrawString("Something", Me.Font, Brushes.DarkOliveGreen, rect, sf)
End Sub