1
我一直在我的程序中使用圖像recgonition,並一直認爲這是我的應用程序中的實際圖像識別代碼的問題。深入研究之後,我意識到我用來創建屏幕截圖的代碼,屏幕的全屏和部分矩形都會創建模糊圖像。圖像模糊,像素化。截屏代碼創建模糊截圖
我通常不會保存圖像,我只是使用內存中的位圖來檢查imagerecognition。雖然以多種格式保存這些屏幕截圖後,我可以看到問題。
這是我用來生成屏幕截圖的代碼。第一個函數創建一個完整的屏幕截圖,第二個從屏幕上的座標創建一個。
Public Shared Function GetScreen() 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)
Using g As Graphics = Graphics.FromImage(screenGrab)
g.CopyFromScreen(New Point(0, 0), New Point(0, 0), screenSize)
Return screenGrab
End Using
End Function
Public Shared Function
GetScreenXY(TopLeft As Point, BottomRight As Point) As Bitmap
Dim w As Integer = BottomRight.X - TopLeft.X
Dim h As Integer = BottomRight.Y - TopLeft.Y
Dim screenGrab As New Bitmap(w, h) 'width and height of the rectangle you want to grab
Using g As Graphics = Graphics.FromImage(screenGrab)
g.CopyFromScreen(TopLeft, New Point(0, 0), screenGrab.Size)
Return screenGrab
End Using
End Function
我在最近添加的應用程序中也有一個截圖工具。它有一個可以調整大小的窗體,它可以截圖並保存。這些也是模糊的。它使用第二個函數。
Visible = False
Dim screenShot As Bitmap = ImageFinder.GetScreenXY(New Point(Left, Top), New Point(Right, Bottom))
Dim sfd As New SaveFileDialog
sfd.Filter = "Jpeg image (*.jpg)|*.jpg|Bitmap image (*.bmp)|*.bmp| PNG image (*.png)|*.png"
sfd.Title = "Save image"
If sfd.ShowDialog() = DialogResult.OK Then
If sfd.FileName <> String.Empty Then
'Set to Jpeg by default.
Dim MyImageFormat As System.Drawing.Imaging.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg
If sfd.FileName.ToString.ToUpper.EndsWith("JPG") Then
MyImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg
ElseIf sfd.FileName.ToString.ToUpper.EndsWith("BMP") Then
MyImageFormat = System.Drawing.Imaging.ImageFormat.Bmp
ElseIf sfd.FileName.ToString.ToUpper.EndsWith("PNG") Then
MyImageFormat = System.Drawing.Imaging.ImageFormat.Png
End If
screenShot.Save(sfd.FileName, MyImageFormat)
是否所有的文件格式看起來模糊或只是JPG格式? –
我測試了你的代碼,它工作正常......嗯。也許上傳一個例子,所以我們可以確切地看到你的意思是「模糊」? –
我只保存爲PNG和BMP。在底部的圖像保存代碼是否有保存圖像爲高品質?如果我在應用程序中截取屏幕截圖並保存並打印出一個屏幕並將其粘貼到繪圖中並以相同格式保存,則打印屏幕版本會更清晰。 – user1632018