Command-line Utility to Save content of clipboard as BMP or JPG file

I'm looking for a lightweight command-line utility that will save the content of the clipboard as a BMP file, or a JPG file where the quality setting of the JPG is either lossless or can be specified on the command-line. The clipboard freeware category seems rather popular, so a tool like this may already exist, in which case a link to download the utility would be sufficient. Otherwise, I would guess that a coded solution in .net or vbscript utilizing an open source component would be fairly straightforward to implement. The intended platform is windows 2003.

The specific application of this utility would be as follows. Using a script, I want to open Excel, copy a range of cells (including any emedded charts) to the clipboard, then save the clipboard as a graphics file for inclusion on a webpage. All these steps need to be done non-interactively. Since I'm not sure about the nature of the data saved to the clipboard when you execute a copy in Excel, a second step where the copied content is pasted-special as a bitmap object, and then that bitmap is copied to the clipboard - this may be even more straightforward. The solution should be more general than this example (i.e. the contents of the clipboard may come from other sources besides Excel).
zacbellAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
0
zacbellAuthor Commented:
I checked that site before posting this question, but have not been able to access the site (Cannot find server or page). Any ideas or know if the binaries are cached somewhere?
0
jkrCommented:
Oh great :-(

I have that archive on my office machine, but I am not sure whether it would be legal to distribute it, and can't check the license since - err, well...

But, since that site used to work for me recently, that might also just be a momentary DNS glitch.
0
zacbellAuthor Commented:
Haven't been able to access the site yet. However, I checked the Google-cached version of the site, and I'm not sure your proposed utility provides the feature I need (of course, the cached description of the utility may not describe all the functionality). Perhaps the latest version (2.2) allows the reverse of the feature described above - i.e. copy clipboard to bmp rather than vice versa.

http://72.14.203.104/search?q=cache:tTpUOPxGedEJ:cmdtools.com/+cmdtools+site:cmdtools.com&hl=en&gl=us&ct=clnk&cd=2

CLIP v2.2 - Store a text file in the Windows clipboard
--------------------------------------------------------------------------------
One of my many jobs is answering questions from other programmers. And I often find the need to write some small code samples, test them, and then send them out as email. I found it annoying to load my source examples into an editor just to get them into the clipboard. So I wrote CLIP to take any filename passed on the command line and the contents of the text file into the clipboard. Version 2.1 now allows you to copy bitmap (.BMP) files to the clipboard.


0
zacbellAuthor Commented:
I decided I didn't like the idea of spending too much time searching for an existing implementation, when I suspected that the implementation should be rather straightforward. I wrote the following source that does precisely what I was looking for, although it does hard-code a cropping action that seems to be required to get rid of some borders that Excel adds the the copied bitmap. If I do end up using other copy sources as I suggested in the prompt, I could parameterize this behavior.

vb.net
---------------------------------------------

Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Imaging

Module ClipOut

    Sub Main(ByVal args As String())

        Dim Data As IDataObject
        Dim Img As Bitmap
        Dim CropImg As Bitmap
        Dim FullFileName As String
        Dim Format As ImageFormat
        Dim DoEncode As Boolean = False
        Dim JpegQuality As Integer
        Dim Params As EncoderParameters

        If args.Length = 0 Or args.Length > 2 Then
            Console.WriteLine("Usage: ClipOut.exe Filename [JpegQuality]")
            Return
        End If

        FullFileName = args(0)

        Select Case System.IO.Path.GetExtension(FullFileName).ToUpper.Substring(1, 3)
            Case "JPG", "JPE"
                Format = ImageFormat.Jpeg
                If args.Length = 2 Then
                    JpegQuality = Integer.Parse(args(1))
                Else
                    JpegQuality = 100
                End If

                DoEncode = True
                Params = New EncoderParameters(1)
                Params.Param(0) = New EncoderParameter(Encoder.Quality, JpegQuality)

            Case "GIF"
                Format = ImageFormat.Gif
            Case "PNG"
                Format = ImageFormat.Png
            Case "TIF"
                Format = ImageFormat.Tiff
            Case Else
                Format = ImageFormat.Bmp
        End Select

        Try

            Data = Clipboard.GetDataObject()

            If Data.GetDataPresent(DataFormats.Bitmap) Then
                Img = Data.GetData(DataFormats.Bitmap)

                ' for some reason, copying an Excel range to the clipboard adds a 1px border on the top and left sides
                CropImg = Crop(Img, 1, 1, Img.Width - 1, Img.Height - 1)

                If DoEncode Then
                    CropImg.Save(FullFileName, FindEncoder(Format), Params)
                Else
                    CropImg.Save(FullFileName, Format)
                End If

                Console.WriteLine("w=" & CropImg.Width.ToString("00000") & ", h=" & CropImg.Height.ToString("00000"))
            Else
                Console.WriteLine("Clipboard does not contain a bitmap image")
            End If

        Catch e As Exception
            Console.WriteLine("Error")
            Return
        End Try

    End Sub

    Private Function FindEncoder(ByVal fmt As ImageFormat) As ImageCodecInfo

        Dim infoary As ImageCodecInfo() = ImageCodecInfo.GetImageEncoders()
        For i As Integer = 0 To infoary.Length - 1
            Dim info As ImageCodecInfo = infoary(i)
            If info.FormatID.Equals(fmt.Guid) Then
                Return info
            End If
        Next

    End Function

    Private Function Crop(ByVal img As Bitmap, ByVal x As Integer, ByVal y As Integer, ByVal width As Integer, ByVal height As Integer) As Bitmap

        Dim NewImg As Bitmap = New Bitmap(width, height, img.PixelFormat)
        Dim Grph As Graphics = Graphics.FromImage(NewImg)

        Grph.DrawImage(img, New Rectangle(0, 0, width, height), New Rectangle(x, y, width, height), GraphicsUnit.Pixel)

        Return NewImg

    End Function

End Module
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.