Click here to Skip to main content
15,891,184 members
Articles / Web Development / HTML

Screen Snaper SideBar Gadget

Rate me:
Please Sign up or sign in to vote.
4.63/5 (18 votes)
1 Dec 2011CPOL2 min read 118.3K   4.7K   74  
Screen Snaper is perfect to quickly share a picture-perfect representation of anything on your screen.
' /*---------------------------------------------------------------------------
'|                                  DCUtility                                  |
'|--------------------------------------+--------------------------------------|
'|    SolutionName : ScreenSnaperGadget                                        |
'|    ProjectName  : ScreenSnaperWrapper                                       |
'|    FileName     : ScreenSnaperWrapper.vb                                    |
'|    CreationDate : 2007-03-07                                                |
'|    Author       : Dany Cantin                                               |
'|    Comments     : dcutility@hotmail.com                                     |
'|--------------------------------------+--------------------------------------|
'|                             Screen Capture Tool                             |
'|                         Windows Vista SideBar Gadget                        |
'|--------------------------------------+--------------------------------------|
'|                          Copyright © 2007 DCUtility                         |
'|                              All rights reserved.                           |
' ---------------------------------------------------------------------------*/

Imports System.Windows.Forms
Imports System.Drawing

<ComClass(ScreenSnaperWrapper.ClassId, ScreenSnaperWrapper.InterfaceId, ScreenSnaperWrapper.EventsId)> _
Public Class ScreenSnaperWrapper

#Region "GUID COM"

    Public Const ClassId As String = "ed869c00-5e06-493c-9bc8-93d77c28195b"
    Public Const InterfaceId As String = "eaf8557c-94a2-4732-8b0b-de87afbdeb1e"
    Public Const EventsId As String = "3089529a-6501-428e-bd94-9be14faf7521"

    Public Sub New()
        MyBase.New()
    End Sub

#End Region

    Public Enum eSnapMode
        ToClipBoard
        ToImage
    End Enum

    Public Function GetSnap(ByVal SnapType As Integer, ByVal SnapMode As eSnapMode) As Boolean
        Dim bReturn As Boolean = True
        Dim hBitmap As IntPtr = IntPtr.Zero

        Try
            hBitmap = GetSnapType(SnapType)

            If Not Equals(hBitmap, IntPtr.Zero) Then

                Select Case SnapMode
                    Case eSnapMode.ToClipBoard
                        bReturn = SnapToClipboard(hBitmap)
                    Case eSnapMode.ToImage
                        bReturn = SnapToImage(hBitmap)
                    Case Else
                        bReturn = False
                End Select
            Else
                bReturn = False
            End If
        Catch ex As Exception
            bReturn = False
        Finally
            If Not Equals(hBitmap, IntPtr.Zero) Then
                DeleteObject(hBitmap)
            End If
        End Try

        Return bReturn
    End Function

    Private Function SnapToClipboard(ByRef hBitmap As IntPtr) As Boolean
        Dim bReturn As Boolean = True

        Try
            Clipboard.SetDataObject(Image.FromHbitmap(hBitmap), True)
        Catch ex As Exception
            bReturn = False
        End Try

        Return bReturn
    End Function

    Private Function SnapToImage(ByRef hBitmap As IntPtr) As Boolean
        Dim bReturn As Boolean = True
        Dim obDialog As SaveFileDialog = Nothing

        Try
            obDialog = New SaveFileDialog

            With obDialog
                .Title = "Save Snap To Image"
                .OverwritePrompt = True
                .FilterIndex = 1
                .FileName = "Image"
                .DefaultExt = "bmp"
                .Filter = "Bitmap Image (*.bmp)|*.bmp|Enhanced Metafile (*.emf)|*.emf|Graphics Interchange Format (*.gif)|*.gif|Joint Photographic Experts Group (*.jpg)|*.jpg|Portable Network Graphics (*.png)|*.png|Tag Image File Format (*.tif)|*.tif|Windows Metafile (*.wmf)|*.wmf"
            End With

            If obDialog.ShowDialog() = DialogResult.OK Then
                Image.FromHbitmap(hBitmap).Save(obDialog.FileName, GetImageFormat(obDialog.FileName))
            Else
                bReturn = False
            End If
        Catch ex As Exception
            bReturn = False
        Finally
            If Not obDialog Is Nothing Then
                obDialog.Dispose()
                obDialog = Nothing
            End If
        End Try

        Return bReturn
    End Function

    Private Function GetImageFormat(ByVal FileName As String) As Imaging.ImageFormat
        Dim Format As Imaging.ImageFormat = Imaging.ImageFormat.Png

        FileName = FileName.ToLower()

        With FileName
            If .EndsWith(".bmp") Then
                Format = Imaging.ImageFormat.Bmp
            ElseIf .EndsWith(".emf") Then
                Format = Imaging.ImageFormat.Emf
            ElseIf .EndsWith(".gif") Then
                Format = Imaging.ImageFormat.Gif
            ElseIf .EndsWith(".jpg") Then
                Format = Imaging.ImageFormat.Jpeg
            ElseIf .EndsWith(".tif") Then
                Format = Imaging.ImageFormat.Tiff
            ElseIf .EndsWith(".wmf") Then
                Format = Imaging.ImageFormat.Wmf
            End If
        End With

        Return Format
    End Function

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO
Canada Canada

Comments and Discussions