2012-06-07 52 views
4

我試圖添加一個功能到我的程序,當他們點擊一個按鈕時,用戶屏幕的完整屏幕截圖。我得到了截圖並打開文件對話框進行保存的程序,保存工作。問題是,無論我如何保存屏幕截圖,保存的圖像都有明顯的質量損失,並且圍繞文本和內容進行了渲染。這是一個巨大的問題,因爲我需要保存的圖像完全像在用戶屏幕上看到的一樣,我不能有任何質量損失。我試圖將圖像保存爲jpg和png,並且都給了我質量損失。我想知道是否有人可以將我指向一些代碼或方法,以使我能夠以與用戶屏幕相同的質量保存屏幕截圖。如果可能,我想將圖像保存爲JPG或PNG格式。任何幫助將不勝感激!高質量完整屏幕截圖VB.Net

+0

失去屏幕截圖的質量,當您使用PNG需要花費相當多的工作質量。使用您正在使用的任何程序的縮放功能來查看圖像。 –

回答

4

以位圖格式獲取圖像並將其保存爲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 
+0

感謝您的快速回復,但我想我忘了提及,如果可能,我想將它保存爲JPG或PNG格式。 – Tony

+2

JPG和PNG不足以存儲圖像的所有細節,所以你會失去質量。 –

+0

好吧,這是令人失望的...所以有沒有辦法解決它? – Tony

1

淨通常保存在96DPI文件,因此使用下面的代碼,你可以保存與JPEG或其它格式更高分辨率的文件。

'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 
+0

@DellDesker,檢查帖子。 –

2

對於初學者來說,JPEG圖像使用有損壓縮算法,因此當您以該格式保存時,您往往會失去質量。最好保存爲未壓縮的位圖(BMP),或使用無損壓縮的PNG

這裏是將屏幕工作區域複製到PNG圖像的代碼。

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 
1

我發現,加入3行以上代碼顯著提高了圖像

var graphics = Graphics.FromImage(theRequestedAllocatedImage); 

graphics.CompositingQuality = CompositingQuality.HighQuality; 
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; 
graphics.SmoothingMode = SmoothingMode.HighQuality; 


// Then call 
graphics.CopyFromScreen(..) 
0
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

請[編輯](https://stackoverflow.com/posts/45788134/edit)你的代碼格式,如果你能解釋更多,那麼答案會更好 –