How to obtain original size of image viewed in Windows.Forms PictureBox.Image - vb.net

I want to know size of a image in a PictureBox, but cannot to access the Image property, because it's nothing.
Dim PiBox1 As New PictureBox
With PiBox1
.ImageLocation = "image.png"
.SizeMode = PictureBoxSizeMode.Zoom
.Dock = DockStyle.fill
If .Image Is Nothing Then MsgBox("Image = Nothing")
End With
PictureBox.Size, PictureBox.ClientSize return dimensions of the control, not the image inside...

While there are other options, to change the code as little as possible, just add a call to the Load method after setting the ImageLocation property. That will force the Image to be loaded synchronously.
That said, you really ought to be setting the Dock and SizeMode properties first. Why would you load an Image first and then change the size afterwards? Do all the configuration first, then load the Image as the last step. This is akin to setting the DataSource last when data-binding.

Related

Take a screenshot of a Control

I want to take a screenshot of a RichTextBox using the following code.
The problem is it takes a screenshot of another part of the Form:
Dim memoryImage As Bitmap
Dim myGraphics As Graphics = Me.CreateGraphics()
Dim s As Size = RichTextBox2.Size
memoryImage = New Bitmap(s.Width, s.Height, myGraphics)
Dim memoryGraphics As Graphics = Graphics.FromImage(memoryImage)
memoryGraphics.CopyFromScreen(RichTextBox2.Bounds.X, RichTextBox2.Bounds.Y, 0, 0, s)
memoryImage.Save(audiooutputfolder & name & ".png")
Graphics.CopyFromScreen() requires that you specify screen coordinates.
You can transform local coordinates into screen coordinates using the Control.RectangleToScreen() and Control.PointToScreen() methods.
Other methods do the opposite, see the Docs.
To compute the client area of a Control in screen coordinates, you can use its RectangleToScreen() method and pass the value of the ClientRectangle property:
Dim clientRectToScreen = [Control].RectangleToScreen([Control].ClientRectangle)
To include the non-client area (e.g., the borders of a Control, including the Scrollbars, if any), you need the screen coordinates of its Bounds.
There are different ways to do this. A simple method is to ask the Parent of a Control to get them, passing to the Parent's RectangleToScreen() method the Bounds of a child Control.
If you want to print a Form, which is a Top-Level Control, so it has no Parent, just use its Bounds directly: these measures already express screen coordinates.
It's shown in the ControlToBitmap() method:
Private Function ControlToBitmap(ctrl As Control, clientAreaOnly As Boolean) As Bitmap
If ctrl Is Nothing Then Return Nothing
Dim rect As Rectangle
If clientAreaOnly Then
rect = ctrl.RectangleToScreen(ctrl.ClientRectangle)
Else
rect = If(ctrl.Parent Is Nothing, ctrl.Bounds, ctrl.Parent.RectangleToScreen(ctrl.Bounds))
End If
Dim img As New Bitmap(rect.Width, rect.Height)
Using g As Graphics = Graphics.FromImage(img)
g.CopyFromScreen(rect.Location, Point.Empty, img.Size)
End Using
Return img
End Function
To take a screenshot of a Control, call this method, passing the Control you want to print to a Bitmap and specify whether you just want its content (the client area) or you want to include the non-client area (for example, if the control to print is a Form, you want to include the Caption and borders).
Important: use Path.Combine() to build a path:
Path.Combine(audiooutputfolder, $"{imageName}.png"
if string interpolation is not available ($"{variable} other parts"), you can glue the file extension to the file name:
Path.Combine(audiooutputfolder, imageName & ".png")
' Get the screenshot, client area only
Dim controlImage = ControlToBitmap(RichTextBox2, True)
' Save the image to the specified Path using the default PNG format
controlImage.Save(Path.Combine(audiooutputfolder, $"{imageName}.png"), ImageFormat.Png)
' [...] when done with the bitmap
controlImage.Dispose()
Side note:
If your app is not DpiAware, you may get wrong screen coordinates.
See these notes about this.

emgu cv save image to file in vb

I have copied a webcam image capture tutorial from the web. It works OK. I want to watch a changing scene and save a captured image to disk when I push a button on the form. The button push is detected, but I am unable to save an image. Here is the main code. I have tried two save methods but neither works. What am I missing?
Sub ProcessFrameAndUpdateGUI(sender As Object, arg As EventArgs)
imgOriginal = capWebcam.QueryFrame() 'get the next frame from the webcam
If (imgOriginal Is Nothing) Then 'if we didn't get a frame
Return
End If
If btnStackPressed = True Then 'is button pressed?
btnStackPressed = False 'clear the button
imgOriginal = capWebcam.QueryFrame() 'get the next frame from the webcam
End If
ibOriginal.Image = imgOriginal 'display the current image in the imagebox
cvSaveImage("C:\imagesaved.bmp", imgOriginal) 'save current image as bmp
imgOriginal.Save("C:/MyPic.jpg") 'save current image as jpg
End Sub
This is sort of an answer. I don't understand the underlying facts. I saw a post on another blog that said the failure to save might be due to a Windows security issue and recommended saving to another disk. This closes the issue for me.
imgOriginal.Save("C:\imgsaved.jpg") 'This doesn't work.
imgOriginal.Save("C:\Photo_temp\imgsaved.jpg") 'This works.
imgOriginal.Save("G:\Photo_temp\imgsaved.jpg") 'This works.
imgOriginal.Save("G:\imgsaved.jpg") 'This works.

Larger Image to fit in picturebox

As you'll can see the first image is the size of (1024*768) and it is correctly displayed in the picturebox and in the second case the image size is (1600*900) and it is displayed to half of the picturebox and the remaining is missing .So No I would like to fir that image in the picturebox no matter what the size is and even though it is greater than the size of the picturebox.I need to scale that Image.So how do I do that?And one more thing is that I need to resize the picturebox automatically when the image loads to it just as we see in the lightbox effect..
http://www.lokeshdhakar.com/projects/lightbox2/ -------->example.
Any help will be appreciated!
Here is what I am getting.
If it's a winforms app, you can set the SizeMode property of the PictureBox to PictureBoxSizeMode.Zoom. Note that this will increase the size of smaller images to fill the frame, so you might want to measure the image first, in order to check if either edge is too long, and then setting SizeMode to either PictureBoxSizeMode.Zoom or PictureBoxSizeMode.Normal.
I know this is marked answered, but I wrote this for one of my own apps. Hope it helps somebody..
Private Sub ScaleImage(ByVal p As PictureBox, ByRef i As Bitmap)
If i.Height > p.Height Then
Dim diff As Integer = i.Height - p.Height
Dim Resized As Bitmap = New Bitmap(i, New Size(i.Width - diff, i.Height - diff))
i = Resized
End If
If i.Width > p.Width Then
Dim diff As Integer = i.Width - p.Width
Dim Resized As Bitmap = New Bitmap(i, New Size(i.Width - diff, i.Height - diff))
i = Resized
End If
End Sub
The two Easiest Ways to Fit an Image to Any Size of PictureBox is:
-to set the Image as Background Image
OR
-to set it as picturebox image then set sizemode to StretchImage
1.Background Image
-use the BackgroundImage property of the PictureBox
picturebox.BackgroundImage = Image.FromStream(New IO.MemoryStream(CType(data, Byte())))
-Then Set its BackgroundImageLayout to stretch Like This:
picturebox.BackgroundImageLayout = ImageLayout.Stretch
Image
-use the Image property of the PictureBox
picturebox.Image = Image.FromStream(New IO.MemoryStream(CType(data, Byte())))
-Then Set its' sizeMode to StretchImage Like This:
picturebox.SizeMode = PictureBoxSizeMode.StretchImage
This will make any Picture / Image / Canvas Stroke (converted to Byte Array) fit according to the height and width of the picturebox
Hope This Helps :)

picture's backgroundcolor in picturebox

how can i change the background color of a picture saving from a picturebox in vb.net.In my form there is a drawing section.after drawing i am saving the picture as jpeg.but the image's background color is black.so i can't see anything that i have drawn.the drawing pen color is also black.if anyone knows please help me.thank you.
I would say that the easiest way is to paint the background when the image object is created (sample initializing the background to being white):
Dim theImage As Image = New Bitmap(someWidth, someHeight)
Using g As Graphics = Graphics.FromImage(theImage)
g.Clear(Color.White)
End Using
To use this the following code:
OpenFileDialog1.Filter = "Bmp Files(*.bmp)|*.bmp|Gif Files(*.gif)|*.gif|Jpg Files(*.jpg)|*.jpg"
OpenFileDialog1.ShowDialog()
Textbox1.Text = OpenFileDialog1.FileName
PicText.Image = Image.FromFile(OpenFileDialog1.FileName)

Saving drawn images in a PictureBox

I created a form with a PictureBox. I created a drawing in the PictureBox. But I can't save the file to a specified folder without dialogue controls. Only through code. Please help me if any one knows a solution for this problem. Thank you....
if you drawing in picture box you must convert drawing in bmp format with this code:
'Save as BMP-file
Dim bmp As New Bitmap(picturebox1.Width, picturebox1.Height)
draw.DrawToBitmap(bmp, New Rectangle(0, 0, picturebox1.Width, picturebox1.Height))
bmp.Save("D:\output.png", Imaging.ImageFormat.Png)
bmp.Dispose()
with this code you can print & save drawing on picturebox object
PictureBox has an Image property of type System.Drawing.Image which you use to get or set the image that the PictureBox represents. System.Drawing.Image has a Save method which will write the image to the specified filename or stream. Is this information OK or do you need more; your question was a little unclear.