Save windows form as image without borders - vb.net

I am currently trying to save a windows form as an image, and I have been able to using the following code:
Dim frm = Me
Using bmp = New Bitmap(frm.Width, frm.Height)
frm.DrawToBitmap(bmp, New Rectangle(0, 0, bmp.Width, bmp.Height))
bmp.Save("D:\programs\files\image.png")
End Using
However, the image includes the borders of the form.
Is there any way to save the image without the borders?

Can you change the border style to none before the picture, then add it back after you save the picture... something like...
Dim frm = Me
frm.FormBorderStyle = FormBorderStyle.None
Using bmp = New Bitmap(frm.Width, frm.Height)
frm.DrawToBitmap(bmp, New Rectangle(0, 0, bmp.Width, bmp.Height))
bmp.Save("D:\programs\files\image.png")
End Using
frm.FormBorderStyle = FormBorderStyle.FixedSingle

Related

Vb.Net Application Screen Capture and Save it as a pdf

I have this small application which capture the screen and save it as a pdf. I am running this application on my PC which has 1080P primary display and 1080P TV as an extended display. When i put the application on the extended TV display (full screen mode) it doesn't capture entire screen. It capture only 1/4 of the screen. But i want to capture the entire screen. How do i fix this. Please help
Here is the code;
Private Sub SaveForm_shift1()
Dim yesterday As String = DateTime.Now.ToString("yyyy-MM-dd")
Dim filePath As String = "C:\Autodesk" + yesterday + ".jpg"
Dim bmpScreenshot As Bitmap = New Bitmap(Width, Height, PixelFormat.Format32bppArgb)
Dim gfxScreenshot As Graphics = Graphics.FromImage(bmpScreenshot)
gfxScreenshot.CopyFromScreen(Me.Location.X, Me.Location.Y, 0, 0, Me.Size, CopyPixelOperation.SourceCopy)
Try
gfxScreenshot.Dispose()
' Save the screenshot
bmpScreenshot.Save(filePath)
saveToPdf_shift1(filePath)
deleteImg(filePath)
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
Use this function to get a Bitmap with all the screens
Public Function GetScreenShot() As Bitmap
Dim bmp As Bitmap = New Bitmap(Screen.AllScreens.Sum(Function(s As Screen) s.Bounds.Width),
Screen.AllScreens.Max(Function(s As Screen) s.Bounds.Height))
Dim gfx As Graphics = Graphics.FromImage(bmp)
gfx.CopyFromScreen(New Point(0, 0), New Point(0, 0), bmp.Size)
Return bmp
End Function
If you have strange staggered screen positions, the empty areas will be black in the resulting bitmap.

Disappearing Picturebox images [duplicate]

This question already has answers here:
Drawing Graphics Disappear in VB.net
(4 answers)
Closed 5 years ago.
I was successful in loading an image from a file into a picturebox in visual basic. Or so i thought. I had a msgbox in the code and the picture showed up in the picturebox. take the msgbox out and no picture. Any Ideas? Thanks
Private Sub DrawImageinSquarePanel(Panelname As PictureBox, ImageFile As String)
g = Panelname.CreateGraphics() 'creates new graphics element in panel
Dim newImage As Image = Image.FromFile(ImageFile) ' Create image.
Dim SquareDim As Integer 'Size of longest dimension in source image
If newImage.Width > newImage.Height Then
SquareDim = newImage.Width
Else
SquareDim = newImage.Height
End If
MsgBox(Panelname.Width & " " & Panelname.Height) 'the magic msgbox!!
' scale factor
Dim imageAttr As New ImageAttributes
imageAttr.SetGamma(Panelname.Width / SquareDim)
Dim ScaleFactor As Single = Panelname.Width / SquareDim
' Create rectangle for source and destination image.
Dim srcRect As New Rectangle(0, 0, newImage.Width, newImage.Height)
Dim destRect As New Rectangle((Panelname.Width - newImage.Width * ScaleFactor) / 2, (Panelname.Height - newImage.Height * ScaleFactor) / 2, newImage.Width * ScaleFactor, newImage.Height * ScaleFactor)
Dim units As GraphicsUnit = GraphicsUnit.Pixel
' Draw image to screen.
g.DrawImage(newImage, destRect, srcRect, units)
End Sub
Your title says "Picturebox" but there's no PictureBox there at all. If you want to display an Image in a PictureBox then assign the Image object to the Image property of that PictureBox.
Don't use GDI+ to draw it on a Panel and, if you ever do use GDI+ to draw, NEVER call CreateGraphics. Always draw on a control in its Paint event handler. The reason that your drawing disappears is that all drawing is erased on every Paint event. By doing your drawing in the Paint event handler, you ensure that it gets reinstated each time.
If you want to modify the image before displaying it then what you should do is create a new Bitmap object, use GDI+ to draw the modified image onto that and then assign that to the Image property of the PictureBox, e.g.
Using originalImage = Image.FromFile(filePath)
Dim newImage As New Bitmap(originalImage.Width, originalImage.Height)
Using g = Graphics.FromImage(newImage)
g.DrawImage(originalImage, Point.Empty)
End Using
'Dispose the existing image if there is one.
PictureBox1.Image?.Dispose()
PictureBox1.Image = newImage
End Using

How to save real PictureBox

I need to save picturebox with real image as displayed.
Help me?
Ex:
Dim myEncoder As Encoder
Dim myImageCodecInfo As ImageCodecInfo
Dim myEncoderParameter As EncoderParameter
Dim myEncoderParameters As EncoderParameters
Dim bmp As Bitmap = CType(imgJpgPng.Image, Bitmap)
Dim bmpt As New Bitmap(640, 640)
Using g As Graphics = Graphics.FromImage(bmpt)
g.DrawImage(bmp, 0, 0, bmpt.Width, bmpt.Height)
End Using
myImageCodecInfo = GetEncoderInfo(ImageFormat.Jpeg)
myEncoder = Encoder.Quality
myEncoderParameters = New EncoderParameters(1)
myEncoderParameter = New EncoderParameter(myEncoder, CType(75L, Int32))
myEncoderParameters.Param(0) = myEncoderParameter
bmpt.Save("d:\ImgTemp\0000.JPG", myImageCodecInfo, myEncoderParameters)
From what I'm getting is that you want to save the picture box image, maybe to a directory or something. So, lets get right into this(It's very simple. By the way, I noticed you have Dim'd the bmpt at 640, 640, maybe that's a problem.
This is how I would save a picture box image
PictureBox1.Image.Save("C:\Users\Owner\Desktop\Webcam Application poctures\Picture.jpg")
Its very self explanatory, Picturebox1.Image.Save(saving the image) and in the quotations, there's the directory, and at the end there' a jpg, you can make it a .png, or any other file format.
If you want to have it being displayed as the original picture in the picture box, you will need to goto the properties of the picture box and make it bigger(size) or use the stretch feature.
Edit: I'm sorry I didn't read your question right, here's an update!
Read this website's information and look into it, this will help you greatly!
http://www.vb-helper.com/howto_resize_picture_save.html

Set picturebox image in thread

i'm trying set picturebox image in thread but i cant set it
take screenshot in specific window
set picturebox image to screenshot
This codes can get screenshot in specific window but cant set picturebox image to it. Whats wrong?
Public Function PrintWindow(hwnd As IntPtr) As Bitmap
Dim rc As RECT
GetWindowRect(hwnd, rc)
Dim bmp As New Bitmap(rc.Width, rc.Height, PixelFormat.Format32bppArgb)
Dim gfxBmp As Graphics = Graphics.FromImage(bmp)
Dim hdcBitmap As IntPtr = gfxBmp.GetHdc()
PrintWindow(hwnd, hdcBitmap, 0)
gfxBmp.ReleaseHdc(hdcBitmap)
gfxBmp.Dispose()
Return bmp
End Function
Dim OverviewRefresherThread As New Thread(AddressOf RefreshOverviewThread)
Public Sub RefreshOverviewThread()
Do
MainWindow.PictureBox.Image = PrintWindow(WindowHandle("TEST"))
Loop
End Sub
thanks...
Am I right? You are tying to get a screenshot from Whatever and transfer it in Picturebox? If so then take a look at this code.
Dim bounds As Rectangle
Dim screenshot As System.Drawing.Bitmap
Dim graph As Graphics
bounds = Screen.PrimaryScreen.Bounds
screenshot = New System.Drawing.Bitmap(bounds.Width, bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
graph = Graphics.FromImage(screenshot)
graph.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy)
PictureBox1.Image = screenshot
This code takes a screenshot of the desktop and transfer it in picbox.
You can see tutorial here.
http://vb6.wonderhowto.com/how-to/capture-desktop-screen-with-vb-net-0158485/
If im not mistaken you already done with the screenshot you just need to transfer it and thats your problem.

VB.NET watermark on a picture

I asked earlier about a way to add a watermark to a picture using visual basic . and thanks to everybody i managed to find a way , but now it seems like I need a code for an option to make the user select the coordinates of the new watermark position on the picture.
Here is my code
Dim Image1 As New Bitmap(OpenFileDialog1.FileName)
Dim Image2 As New Bitmap(PictureBox1.Image)
Dim Result As New Bitmap(Math.Max(Image1.Width, Image2.Width), _
Image1.Height + Image2.Height)
Dim gResult As Graphics = Graphics.FromImage(Result)
gResult.DrawImage(Image1, New Point(0, 0))
gResult.DrawImage(Image2, New Point(0, Image1.Height))
gResult.Dispose()
gResult = Nothing
PictureBox5.Image = Result
i found a way to do it . you can use 2 pictureboxes and add a code to move the watermark picturebox . you can save everything at the end using a screenshot method .