We help IT Professionals succeed at work.

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

zacbell asked
Medium Priority
Last Modified: 2013-12-03
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).
Watch Question

Top Expert 2012



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?
Top Expert 2012

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.


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.

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.

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.


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]")
        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))
                    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


            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)
                    CropImg.Save(FullFileName, Format)
                End If

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

        Catch e As Exception
        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

    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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.