2013-01-06 113 views
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) 
+0

是否所有的文件格式看起來模糊或只是JPG格式? –

+0

我測試了你的代碼,它工作正常......嗯。也許上傳一個例子,所以我們可以確切地看到你的意思是「模糊」? –

+0

我只保存爲PNG和BMP。在底部的圖像保存代碼是否有保存圖像爲高品質?如果我在應用程序中截取屏幕截圖並保存並打印出一個屏幕並將其粘貼到繪圖中並以相同格式保存,則打印屏幕版本會更清晰。 – user1632018

回答

0

這裏是我的截屏的方式,它需要他們非常漂亮,我想我會與你分享。

'Declare my constants here for screenshots' 
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer) 
Private Const VK_SNAPSHOT As Short = &H2CS 

'Create my function to take the picture of the screen' 
Public Function SaveScreen(ByVal theFile As String) As Boolean 
    Try 
     Dim data As IDataObject 
     data = Clipboard.GetDataObject() 
     Dim bmap As Bitmap 
     If data.GetDataPresent(GetType(System.Drawing.Bitmap)) Then 
      bmap = CType(data.GetData(GetType(System.Drawing.Bitmap)), Bitmap) 
      Me.picScreen.Image = bmap 
      Me.picScreen.Image.Save(theFile, Imaging.ImageFormat.Jpeg) 
     End If 
    Catch s As Exception 
    End Try 
End Function 

現在,讓我們保存此圖片...

Private Sub btnSavePic_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSavePic.Click 
    Try 

      Call keybd_event(System.Windows.Forms.Keys.Snapshot, 0, 0, 0) 
      System.Threading.Thread.Sleep(200) ' To have time to catch the clipboard 
      SaveScreen("YOURFILELOCATION" & Format(Now, "MM-dd-yyyy ") & " " & Format(TimeOfDay, "hhmmss") & ".bmp") 

    Catch w As Exception 
    End Try 
End Sub 

給它一個鏡頭,我用這個,我的屏幕報告給我,當我離去......