Referencing screen height and width in vb.net - vb.net

How do I reference the screen height and width in vb.net? For example, the bottom right corner's locations, the top right corner's locations, etc.
I tried My.Computer.Screen but couldnt find anything that told me the size.

You can use:
My.Computer.Screen.Bounds
or:
Screen.PrimaryScreen.Bounds
Bounds is a rectangle that provides the size. Alternatively, you can look at the WorkingArea, which will not include the task bar and docked windows.

You can use something like:
My.Computer.Screen.Bounds.Size.Width
My.Computer.Screen.Bounds.Size.Height

For WPF you can use:
System.Windows.SystemParameters.PrimaryScreenWidth
System.Windows.SystemParameters.PrimaryScreenHeight

dim height as integer = Screen.PrimaryScreen.Bounds.Height
dim width as integer = Screen.PrimaryScreen.Bounds.Width

Insert this code into form_load. I put some resolutions...
Dim dw As Double
Dim dh as Double
Width = Screen.PrimaryScreen.Bounds.Width
If (Width = 1366) Then
dw = 1
ElseIf (Width = 1920) Then
dw = 1.4055
ElseIf (Width = 1280) Then
dw = 0.9379
End If
For Each c As Control In Me.Controls
c.Width = CInt(CDbl(c.Width * dw))
Next
Height = My.Computer.Screen.Bounds.Size.Height
If (Height = 768) Then
dh = 1
ElseIf (Height = 1080) Then
dh = 1.4062
ElseIf (Height = 1024) Then
dh = 1.3333
End If
For Each g As Control In Me.Controls
g.Height = CInt(CDbl(g.Height * dh))
Next

Related

Resizing controls in a form

I have 3 controls and wanted to resize and reposition them based on what percentage increase or decrease the form WindowWidth property is. I came up with two formulas
One for increasing the .width and .Left properties and the other is for reducing the .width and .Left properties.
The form consists of a subform which is anchored to stretch down and across. All the textboxes are anchored to the left.
The initial size of the form is 20.5cm = 11624 twips
The anchored and maximised size of the form becomes 36.01cm = 20415 twips
These values are found using Me.Width and Me.WindowWidth properties respectively.
I want to be able to use the Me.Width and Me.WindowWidth properties to manipulate the position and width of the controls when the form gets resized.
I have the below code which works on form load but does not work if a user tries to resize using the access window. Any help or direction is highly welcome.
Private Sub Form_Resize()
Dim widthFactor As Double
If Me.WindowWidth = Me.Width Then
Exit Sub
widthFactor = 1 - ((Me.WindowWidth - Me.Width) / Me.WindowWidth) 'for decreasing the controls size and position
Me.txtFirst.Left = Me.txtFirst.Left * widthFactor
Me.txtSecond.Left = Me.txtSecond.Left * widthFactor
Me.txtThird.Left = Me.txtThird.Left * widthFactor
Me.txtFirst.Width = Me.txtFirst.Width * widthFactor
Me.txtSecond.Width = Me.txtSecond.Width * widthFactor
Me.txtThird.Width = Me.txtThird.Width * widthFactor
ElseIf Me.Width < Me.WindowWidth Then
widthFactor = ((Me.WindowWidth - Me.Width) / Me.Width) + 1 ' for increasing the controls size and position
Me.txtFirst.Left = Me.txtFirst.Left * widthFactor
Me.txtSecond.Left = Me.txtSecond.Left * widthFactor
Me.txtThird.Left = Me.txtThird.Left * widthFactor
Me.txtFirst.Width = Me.txtFirst.Width * widthFactor
Me.txtSecond.Width = Me.txtSecond.Width * widthFactor
Me.txtThird.Width = Me.txtThird.Width * widthFactor
End If
End Sub

Do I have to DISPOSE and how should I do it?

I zoom an image with the following code
While PictureBox1.Image.Height < ScreenHeight
PictureBox1.Image = New Bitmap(Image1, PictureBox1.Image.Width * 1.003, PictureBox1.Image.Height * 1.003)
Me.PictureBox1.Update()
End While
Do I need to dispose within the loop (but how do I preserve my image then?) or is it sufficient to dispose only once outside the loop?
Do I use Picturebox1.Image.Dispose()
Do I also need to use PictureBox1.Image = Nothing
Thanks for any useful information
This seems to work well
Dim Width, Height As Integer
Width = PictureBox1.Image.Width
Height = PictureBox1.Image.Height
While Height < ScreenHeight And (Microsoft.VisualBasic.DateAndTime.Timer - StartTime) < ScreenSaverDuration
PictureBox1.Image = New Bitmap(Image1, Width * 1.003, Height * 1.003)
Width = PictureBox1.Image.Width
Height = PictureBox1.Image.Height
Me.PictureBox1.Update()
PictureBox1.Image.Dispose()
End While
Dim Height As Integer = PictureBox1.Image.Height
Dim Width As Integer = PictureBox1.Image.Width
While PictureBox1.Image.Height < ScreenHeight
Height *= 1.003
Width *= 1.003
End While
PictureBox1.Image = New Bitmap(Image1, Width, Height)
PictureBox1.Update()

How Can I re-size an image in VB.Net

I need it so that in my code if something evaluates to true it changes the image location and size.
This is my code so far:
With picValueTwentySix
.Location = New Point(302, 134)
.Size = New System.Drawing.Size(169, 40)
.SizeMode = PictureBoxSizeMode.Zoom
End With
Anybody know why it isn't re-sizing?
Thanks!
As everyone has already mentioned, you need to work the the image. Here is a function I made up for ease of use.
Public Function ResizeImage(ByVal image As Image, ByVal size As Size, Optional ByVal preserveAspectRatio As Boolean = True) As Image
Try
Dim newWidth As Integer
Dim newHeight As Integer
If preserveAspectRatio Then
Dim originalWidth As Integer = image.Width
Dim originalHeight As Integer = image.Height
Dim percentWidth As Single = CSng(size.Width) / CSng(originalWidth)
Dim percentHeight As Single = CSng(size.Height) / CSng(originalHeight)
Dim percent As Single = IIf(percentHeight < percentWidth, percentHeight, percentWidth)
newWidth = CInt(originalWidth * percent)
newHeight = CInt(originalHeight * percent)
Else
newWidth = size.Width
newHeight = size.Height
End If
Dim newImage As Image = New Bitmap(newWidth, newHeight)
Using graphicsHandle As Graphics = Graphics.FromImage(newImage)
graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic
graphicsHandle.DrawImage(image, 0, 0, newWidth, newHeight)
End Using
Return newImage
Catch ex As Exception
Return image
End Try
End Function
Basically it creates a new blank graphic to the dimensions you request, then copies the original image to it while scaling it to fit. I think if you step throw it a line at a time you should be pretty self explanatory, but ask if you have questions...
As stated by #Plutonix, changing the Picturebox size will not affect the image size itself, you have to make sure the actual image size is bigger than the size of the picture box, set the size mode of the picturebox to stretchimage, in this case once you resize the picture box the change will reflect. Also refresh the picture box after resizing.

VB.NET - Create An Image - Resize But Add Background To Remainding Space

I am attempting to resize an image to specific dimensions but I do not want to stretch the image at all if it is smaller than my chosen dimensions. Instead I want to add a black background around the image area that is not in use.
I think that the easiest way to do this would be to create a new image of my desired dimensions & set a background color & then add & center the image over top of this background.
I have created a Bitmap using:
Dim bmp As New Drawing.Bitmap(500, 500)
Dim grap As Drawing.Graphics = Drawing.Graphics.FromImage(bmp)
grap.Clear(Drawing.Color.Black)
From this point I got a bit lost on how to complete the process, all that is needed is to add an image to the Bitmap & center.
Any ideas would be much appretiated
I haven't tested this but it looks like you've pretty much got what you want but:
grap.Clear(Drawing.Color.Black)
Will surely just wipe the entire graphic back to black.
Try doing the clear prior to drawing image:
Graphics pic = this.CreateGraphics();
pic.Clear(Color.Black);
pic.DrawImage(img, new Point(center));
Ended up using:
' Load Image
Dim FilePath As String = "testimage.jpg"
Dim OriginalImage As New Bitmap(FilePath)
' Resize Image While Maintaining Aspect Ratio
Dim aspectRatio As Double
Dim newHeight As Integer
Dim newWidth As Integer
Dim maxWidth As Integer = 500
Dim maxHeight As Integer = 500
' Calculate Size
If OriginalImage.Width > maxWidth Or OriginalImage.Height > maxHeight Then
If OriginalImage.Width >= OriginalImage.Height Then ' image is wider than tall
newWidth = maxWidth
aspectRatio = OriginalImage.Width / maxWidth
newHeight = CInt(OriginalImage.Height / aspectRatio)
Else ' image is taller than wide
newHeight = maxHeight
aspectRatio = OriginalImage.Height / maxHeight
newWidth = CInt(OriginalImage.Width / aspectRatio)
End If
Else ' if image is not larger than max then increase size
If OriginalImage.Width > OriginalImage.Height Then
newWidth = maxWidth
aspectRatio = OriginalImage.Width / maxWidth
newHeight = CInt(OriginalImage.Height / aspectRatio)
Else
newHeight = maxHeight
aspectRatio = OriginalImage.Height / maxHeight
newWidth = CInt(OriginalImage.Width / aspectRatio)
End If
' Below keeps original height & width instead of resizing to fit new height / width
' newWidth = OriginalImage.Width
' newHeight = OriginalImage.Height
End If
Dim newImg As New Bitmap(OriginalImage, CInt(newWidth), CInt(newHeight)) '' blank canvas
' Create New Bitmap
Dim bmp As New Drawing.Bitmap(500, 500)
Dim grap As Drawing.Graphics = Drawing.Graphics.FromImage(bmp)
grap.Clear(Drawing.Color.Black)
Dim g As Graphics = Graphics.FromImage(bmp)
' Calculate Points To Insert Resized Image
Dim InsertX As Integer
Dim InsertY As Integer
' Calculate Y Axis Point
If newImg.Height >= 500 Then
InsertY = 0
Else
InsertY = CInt(((500 - newImg.Height) / 2))
End If
' Calculate X Axis Point
If newImg.Width >= 500 Then
InsertX = 0
Else
InsertX = CInt(((500 - newImg.Width) / 2))
End If
' Add Resized Image To Canvas
g.DrawImage(newImg, New Point(InsertX, InsertY))
By using just the ratio of the larger axis, you can fail in the situation that the other ratio forces you to exceed the other axis.
i.e. 1400x1000 ->( i want to fit into 300x200) -> but just with 1400/300 ratio (4.6) the result will be 300x214.
I think it would be useful to check both ratios and continue with the bigger

Resize PictureBox to match image size

How to resize the Picturebox so it can show the full image if the image size is less than monitor size ! I wrote a code which can not resize (but still posting the code)!
Code before loading image on a button click
Dim bmp As Bitmap
bmp = New Bitmap(path)
If bmp.Width < picBox.Image.Width Then picBox.Width = bmp.Width : If bmp.Height < picBox.Image.Height Then picBox.Height = bmp.Height
picBox.Invalidate() : picBox.Refresh()
'picBox.SetBounds(x,y,width,height)
The code does not resize the picturebox, it's just untouched !
Edit
The form has the picBox and a groupbox [dock enabled] control only.
bmp = New Bitmap(dlgOpen.FileName)
picBox.SizeMode = PictureBoxSizeMode.Normal
Dim w As Integer = picBox.ClientSize.Width
Dim h As Integer = picBox.ClientSize.Height
If bmp.Width > w Then
w = bmp.Width
End If
If bmp.Height > h Then
h = bmp.Height
End If
If w > Me.Width - grpBox.Width Then
w = Me.Width - grpBox.Width
End If
If h > grpBox.Height Then
h = grpBox.Height
End If
picBox.ClientSize = New Size(w, h)
picBox.ImageLocation = dlgOpen.FileName
This code does not re-sizes the picture box either .
In .NET, there's the Public Enumeration PictureBoxSizeMode that allows you to change how the PictureBox handles differently sized images:
Normal
StretchImage
AutoSize
CenterImage
Zoom
You can set it for the current PictureBox via the .SizeMode property. AutoSize is probably what you are looking for. If it is larger than the window or frame, you will have to handle this in a PictureBox.Resize event to either resize the window or rescale the image.
So, it might be something like:
Dim bmp As Bitmap
bmp = New Bitmap(path)
picBox.SizeMode = PictureBoxSizeMode.AutoResize
picBox.Image = bmp
Dim bmp As New Bitmap(path)
PictureBox1.SizeMode = PictureBoxSizeMode.Normal
Dim w As Integer = PictureBox1.ClientSize.Width
Dim h As Integer = PictureBox1.ClientSize.Height
If bmp.Width > w Then
w = bmp.Width
End If
If bmp.Height > h Then
h = bmp.Height
End If
If w > maxWidth Then
w = maxWidth
End If
If h > maxHeight Then
h = maxHeight
End If
PictureBox1.ClientSize = New Size(w, h)
PictureBox1.Image = bmp
Picture-box can be re-sized only before loading the image, there after it is read only and has no effect !