High Quality Full Screenshots VB.Net

I am trying to add a function to my program in order to take a full screenshot of the user's screen when they press the button. I got a program to take a screenshot and open a file dialog to save it. The problem is that no matter how I save the screenshot, the saved image has a significant loss of quality and pixels around the text and so on. This is a serious problem, because I need the image to be saved exactly as it is visible on the user's screen, I cannot get ANY quality loss whatsoever. I tried to save the image as jpg and png, and both gave me a loss of quality. I was wondering if anyone could point me to some code or method that would allow me to save screenshots with the same quality as the users screen. I would like to save the image as jpg or png if possible. Any help would be greatly appreciated!

+4
source share
5 answers

Get the image in Bitmap format and save it as bmp.

Private Function TakeScreenShot() As Bitmap Dim screenSize As Size = New Size(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height) Dim screenGrab As New Bitmap(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height) Dim g As Graphics = Graphics.FromImage(screenGrab) g.CopyFromScreen(New Point(0, 0), New Point(0, 0), screenSize) Return screenGrab End Function 
+4
source

To start, JPEG images use a lossy compression algorithm, so you lose quality when saving in this format. It is advisable to save as a Bitmap (BMP), which is uncompressed, or PNG , which uses lossless compression.

Here is the code to copy the work area of ​​the screen to a PNG image.

 Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click 'the working area excludes all docked toolbars like taskbar, etc. Dim currentScreen = Screen.FromHandle(Me.Handle).WorkingArea 'create a bitmap of the working area Using bmp As New Bitmap(currentScreen.Width, currentScreen.Height) 'copy the screen to the image Using g = Graphics.FromImage(bmp) g.CopyFromScreen(New Point(0, 0), New Point(0, 0), currentScreen.Size) End Using 'save the image Using sfd As New SaveFileDialog() With {.Filter = "PNG Image|*.png", .InitialDirectory = My.Computer.FileSystem.SpecialDirectories.Desktop} If sfd.ShowDialog() = Windows.Forms.DialogResult.OK Then bmp.Save(sfd.FileName, System.Drawing.Imaging.ImageFormat.Png) End If End Using End Using End Sub 
+2
source

.Net usually saves the file at 96dpi, so using the following code, you can save the file in higher resolution using Jpeg or another format.

 'Create a new bitmap Using Bmp As New Bitmap(800, 1000, Imaging.PixelFormat.Format32bppPArgb) 'Set the resolution to 300 DPI Bmp.SetResolution(300, 300) 'Create a graphics object from the bitmap Using G = Graphics.FromImage(Bmp) 'Paint the canvas white G.Clear(Color.White) 'Set various modes to higher quality G.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic G.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias G.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias 'Create a font Using F As New Font("Arial", 12) 'Create a brush Using B As New SolidBrush(Color.Black) 'Draw some text G.DrawString("Hello world", F, B, 20, 20) End Using End Using End Using 'Save the file as a TIFF Bmp.Save("c:\\test.Jpeg", Imaging.ImageFormat.Jpeg) End Using 
+1
source

I found that adding 3 lines to the above code significantly improves image quality

 var graphics = Graphics.FromImage(theRequestedAllocatedImage); graphics.CompositingQuality = CompositingQuality.HighQuality; graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.SmoothingMode = SmoothingMode.HighQuality; // Then call graphics.CopyFromScreen(..) 
+1
source
 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim screenSize As Size = New Size(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height) Dim screenGrab As New Bitmap(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height) Dim g As Graphics = Graphics.FromImage(screenGrab) g.CopyFromScreen(New Point(0, 0), New Point(0, 0), screenSize) PictureBox1.Image = screenGrab PictureBox1.Image.Save("c:\picture.bmp") End Sub 
0
source

Source: https://habr.com/ru/post/1416545/


All Articles