Convert Figma logo to code with AI

kbinani logoscreenshot

Go library to capture desktop to image

1,305
179
1,305
3

Top Related Projects

2,467

ksnip the cross-platform screenshot and annotation tool

25,214

Powerful yet simple to use screenshot software :desktop_computer: :camera_flash:

30,110

ShareX is a free and open source program that lets you capture or record any area of your screen and share it with a single press of a key. It also allows uploading images, text or other types of files to many supported destinations you can choose from.

Greenshot for Windows - Report bugs & features go here: https://greenshot.atlassian.net or look for information on:

Quick Overview

kbinani/screenshot is a Go library that provides cross-platform screen capture functionality. It supports Windows, macOS, and X11-based systems, allowing developers to easily capture screenshots programmatically in their Go applications.

Pros

  • Cross-platform support (Windows, macOS, X11)
  • Simple and easy-to-use API
  • Lightweight with minimal dependencies
  • Supports capturing specific displays or all displays

Cons

  • Limited image format support (only PNG)
  • No built-in image manipulation features
  • Requires CGo for macOS and X11 platforms
  • May have performance limitations for high-frequency captures

Code Examples

Capture all displays:

package main

import (
    "fmt"
    "github.com/kbinani/screenshot"
    "image/png"
    "os"
)

func main() {
    n := screenshot.NumActiveDisplays()
    for i := 0; i < n; i++ {
        bounds := screenshot.GetDisplayBounds(i)
        img, err := screenshot.CaptureRect(bounds)
        if err != nil {
            panic(err)
        }
        fileName := fmt.Sprintf("screen%d.png", i)
        file, _ := os.Create(fileName)
        png.Encode(file, img)
        file.Close()
    }
}

Capture a specific display:

package main

import (
    "github.com/kbinani/screenshot"
    "image/png"
    "os"
)

func main() {
    bounds := screenshot.GetDisplayBounds(0)
    img, err := screenshot.CaptureRect(bounds)
    if err != nil {
        panic(err)
    }
    file, _ := os.Create("screenshot.png")
    png.Encode(file, img)
    file.Close()
}

Capture a custom rectangle:

package main

import (
    "github.com/kbinani/screenshot"
    "image"
    "image/png"
    "os"
)

func main() {
    bounds := image.Rect(100, 100, 500, 500)
    img, err := screenshot.CaptureRect(bounds)
    if err != nil {
        panic(err)
    }
    file, _ := os.Create("custom_area.png")
    png.Encode(file, img)
    file.Close()
}

Getting Started

To use the kbinani/screenshot library in your Go project, follow these steps:

  1. Install the library:

    go get github.com/kbinani/screenshot
    
  2. Import the library in your Go code:

    import "github.com/kbinani/screenshot"
    
  3. Use the provided functions to capture screenshots:

    img, err := screenshot.CaptureDisplay(0)
    if err != nil {
        panic(err)
    }
    // Process or save the captured image
    

Remember to handle errors and properly close any opened files when saving screenshots.

Competitor Comparisons

2,467

ksnip the cross-platform screenshot and annotation tool

Pros of ksnip

  • More feature-rich, offering annotation tools and image editing capabilities
  • Cross-platform support (Linux, Windows, macOS)
  • Active development with frequent updates and bug fixes

Cons of ksnip

  • Larger codebase and more complex installation process
  • Potentially higher resource usage due to additional features
  • Steeper learning curve for users who only need basic screenshot functionality

Code Comparison

ksnip (C++):

void KsnipConfig::setCaptureCursor(bool captureCursor)
{
    mConfig->setValue(QStringLiteral("captureCursor"), captureCursor);
    emit captureCursorChanged(captureCursor);
}

screenshot (Go):

func CaptureWindow(hwnd win.HWND) (*image.RGBA, error) {
    rect, err := GetWindowRect(hwnd)
    if err != nil {
        return nil, err
    }
    return CaptureRect(rect)
}

The code snippets demonstrate different approaches to screenshot functionality. ksnip uses Qt for configuration and signal handling, while screenshot utilizes Go's native libraries for capturing window content. ksnip's code suggests more advanced features, while screenshot focuses on simplicity and efficiency.

25,214

Powerful yet simple to use screenshot software :desktop_computer: :camera_flash:

Pros of Flameshot

  • More feature-rich with advanced editing tools and annotations
  • Cross-platform support (Linux, Windows, macOS)
  • Active development and community support

Cons of Flameshot

  • Larger codebase and potentially more complex to use
  • May have higher resource usage due to additional features

Code Comparison

Flameshot (C++):

void CaptureWidget::mouseReleaseEvent(QMouseEvent *e) {
    if (e->button() == Qt::RightButton) {
        m_rightClick = true;
        m_selection->saveGeometry();
        hide();
        close();
    }
}

Screenshot (Go):

func CaptureScreen(displayIndex int) (*image.RGBA, error) {
    display, err := getDisplay(displayIndex)
    if err != nil {
        return nil, err
    }
    return captureDisplay(display)
}

Summary

Flameshot is a more comprehensive screenshot tool with advanced features and cross-platform support, while Screenshot is a simpler, lightweight option focused on basic screen capture functionality. Flameshot's codebase is in C++ and offers more complex user interactions, whereas Screenshot uses Go and provides a straightforward API for capturing screens. The choice between the two depends on the specific requirements of the project and the desired level of functionality.

30,110

ShareX is a free and open source program that lets you capture or record any area of your screen and share it with a single press of a key. It also allows uploading images, text or other types of files to many supported destinations you can choose from.

Pros of ShareX

  • More comprehensive feature set, including screen recording, file sharing, and productivity tools
  • Extensive customization options and plugins
  • Active development with frequent updates and a large community

Cons of ShareX

  • Larger application size and potentially higher resource usage
  • Steeper learning curve due to the abundance of features
  • Windows-only, limiting cross-platform compatibility

Code Comparison

ShareX (C#):

public static Image CaptureRectangle(Rectangle rectangle)
{
    using (Bitmap bmp = new Bitmap(rectangle.Width, rectangle.Height))
    using (Graphics g = Graphics.FromImage(bmp))
    {
        g.CopyFromScreen(rectangle.Location, Point.Empty, rectangle.Size);
        return (Image)bmp.Clone();
    }
}

screenshot (C++):

void Window::captureRect(int x, int y, int width, int height)
{
    CGRect rect = CGRectMake(x, y, width, height);
    CGImageRef image = CGWindowListCreateImage(rect, kCGWindowListOptionOnScreenOnly, kCGNullWindowID, kCGWindowImageDefault);
    // ... (further processing of the captured image)
}

The code snippets show different approaches to capturing screenshots, with ShareX using C# and the .NET framework, while screenshot uses C++ with macOS-specific APIs.

Greenshot for Windows - Report bugs & features go here: https://greenshot.atlassian.net or look for information on:

Pros of Greenshot

  • More feature-rich, offering advanced editing capabilities and plugins
  • Cross-platform support (Windows, macOS, Linux)
  • Active development with frequent updates and bug fixes

Cons of Greenshot

  • Larger codebase and more complex architecture
  • Potentially higher resource usage due to additional features
  • Steeper learning curve for users who only need basic screenshot functionality

Code Comparison

Greenshot (C#):

public void CaptureScreenshot()
{
    Rectangle bounds = Screen.GetBounds(Point.Empty);
    using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
    {
        using (Graphics g = Graphics.FromImage(bitmap))
        {
            g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
        }
        bitmap.Save("screenshot.png", ImageFormat.Png);
    }
}

Screenshot (C++):

void captureScreenshot()
{
    auto image = CGWindowListCreateImage(CGRectInfinite, kCGWindowListOptionOnScreenOnly, kCGNullWindowID, kCGWindowImageDefault);
    CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, CFSTR("screenshot.png"), kCFURLPOSIXPathStyle, false);
    CGImageDestinationRef destination = CGImageDestinationCreateWithURL(url, kUTTypePNG, 1, NULL);
    CGImageDestinationAddImage(destination, image, nil);
    CGImageDestinationFinalize(destination);
}

Both repositories provide screenshot functionality, but Greenshot offers a more comprehensive solution with additional features and cross-platform support. Screenshot, on the other hand, focuses on a simpler, lightweight approach for capturing screenshots.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

screenshot

Go Report Card

  • Go library to capture desktop screen.
  • Multiple display supported.
  • Supported GOOS: windows, darwin, linux, freebsd, openbsd, and netbsd.
  • cgo free except for GOOS=darwin.

example

  • sample program main.go

    package main
    
    import (
    	"github.com/kbinani/screenshot"
    	"image/png"
    	"os"
    	"fmt"
    )
    
    func main() {
    	n := screenshot.NumActiveDisplays()
    
    	for i := 0; i < n; i++ {
    		bounds := screenshot.GetDisplayBounds(i)
    
    		img, err := screenshot.CaptureRect(bounds)
    		if err != nil {
    			panic(err)
    		}
    		fileName := fmt.Sprintf("%d_%dx%d.png", i, bounds.Dx(), bounds.Dy())
    		file, _ := os.Create(fileName)
    		defer file.Close()
    		png.Encode(file, img)
    
    		fmt.Printf("#%d : %v \"%s\"\n", i, bounds, fileName)
    	}
    }
    
  • output example

    $ go run main.go
    #0 : (0,0)-(1280,800) "0_1280x800.png"
    #1 : (-293,-1440)-(2267,0) "1_2560x1440.png"
    #2 : (-1373,-1812)-(-293,108) "2_1080x1920.png"
    $ ls -1
    0_1280x800.png
    1_2560x1440.png
    2_1080x1920.png
    main.go
    

coordinate

Y-axis is downward direction in this library. The origin of coordinate is upper-left corner of main display. This means coordinate system is similar to Windows OS

license

MIT Licence

author

kbinani