MIT License

项目介绍

clowd/Clowd.Clipboard 是一个托管在 GitHub 上的开源项目,主要使用 C# 编写,采用 MIT License 许可证,目前获得 51 个 Star、3 次 Fork。

仓库全名
clowd/Clowd.Clipboard
开源许可证
MIT License
Star 数
51
Fork 数
3

README 摘要

Clowd.Clipboard This library is a light-weight clipboard replacement library for dotnet. It is not tightly coupled to a UI framework, and it contains a custom bitmap parser to help deal with some of the inconsistencies which are present in clipboard images. Package | Nuget | Notes | Clipboard Examples Below are several examples of how to do clipboard operations. Getting or setting simple types (single operation) The…

完整内容见 GitHub 原始 README 或本页 。

常见问题

Clowd.Clipboard 用什么语言开发?

主要使用 C# 编写,采用 MIT License 许可证,目前有 51 个 Star。

Clowd.Clipboard 可以免费商用吗?

仓库采用 MIT License 许可证,可免费使用与修改,商业使用请遵守许可证条款(例如保留版权声明与许可声明)。

如何获取 Clowd.Clipboard 的源码?

可以直接克隆仓库:git clone https://github.com/clowd/Clowd.Clipboard.git,默认分支为 master;安装与运行步骤见仓库 README。

Clowd.Clipboard 最近还在维护吗?

仓库最近一次更新为 8月14日,当前没有未关闭的 Issue;Star 数为 51,可作为社区活跃度的参考。

关于

GitHub 原始描述(英文)

A windows clipboard library for dotnet, and a tailored bmp/dib parser.

描述取自 GitHub 官方 API,保持仓库作者书写的原文。

简要分析

热度分
60
价值分
31
活跃状态
活跃

语言占比

C#
Shell

贡献者

README

Clowd.Clipboard

This library is a light-weight clipboard replacement library for dotnet. It is not tightly coupled to a UI framework, and it contains a custom bitmap parser to help deal with some of the inconsistencies which are present in clipboard images.

Join us on Discord: Discord

Package Nuget Notes
Clowd.Clipboard Nuget Core library containing basic clipboard functionality. No image/bitmap support.
Clowd.Clipboard.Gdi Nuget Adds ClipboardGdi, images using System.Drawing.Bitmap.
Clowd.Clipboard.Wpf Nuget Adds ClipboardWpf, images using System.Windows.Media.Imaging.BitmapSource.
Clowd.Clipboard.Avalonia Nuget Adds ClipboardAvalonia, images using Avalonia.Media.Imaging.Bitmap.

Clipboard Examples

Below are several examples of how to do clipboard operations.

Getting or setting simple types (single operation)

The static helper (eg. ClipboardWpf or ClipboardGdi) has methods for simple things. This takes care of opening the clipboard, reading the data, and then disposing the handle.

// get text on clipboard
string clipboardText = await ClipboardWpf.GetTextAsync();

// get image on clipboard
BitmapSource clipboardImg = await ClipboardWpf.GetImageAsync();

// set image on clipboard (this clears what was previously on clipboard)
ClipboardWpf.SetImage(clipboardImg);

Getting or setting complex types (multi operation)

You should not use the static helper methods if you need to perform several clipboard operations. If you need to read or set multiple formats at once, or check for what formats are available, you should instead open a clipboard handle and dispose it when you’re done.

BitmapSource clipImage;
string clipText;

using (var handle = await ClipboardWpf.OpenAsync());
{
    if (handle.ContainsText()) 
    {
        clipText = handle.GetText();
    }
    
    if (handle.ContainsImage())
    {
        clipImage = handle.GetImage();
    }
}

Reading list of currently available clipboard formats

using (var handle = await ClipboardWpf.OpenAsync());
{
    Console.WriteLine("Formats currently on the clipboard: ");
    foreach (var f in handle.GetPresentFormats())
        Console.WriteLine(" - " + f.Name);
}

Using custom clipboard formats

Using a custom/application specific format is very easy with this library. You should first register your format somewhere statically in your application.

// a custom format stored on the clipboard as UTF-8. 
// the name chosen for the format should be globally unique, so consider
// using a guid if you do not intend to share with other applications.
private static readonly ClipboardFormat<string> myCustomTextFormat 
    = ClipboardFormat.CreateCustomFormat("MyGloballyUniqueFormatId", new TextUtf8());

// a custom format for storing binary data
private static readonly ClipboardFormat myCustomBinaryFormat 
    = ClipboardFormat.CreateCustomFormat("MyGloballyUniqueFormatId_2");

Once your clipboard format is registered, you can use it the same way as any built-in format.

// set custom data to clipboard
using (var handle = await ClipboardWpf.OpenAsync())
{
    // it is possible to set multiple items to the clipboard.
    handle.SetFormat(myCustomTextFormat, "My Custom Text");
    handle.SetFormat(myCustomBinaryFormat, new byte[0]);

    // this is a common "built-in" specific format not covered by the simple functions such as "GetText".
    handle.SetFormat(ClipboardFormat.Html, "<html>Some Html to share with other applications</html>");
}

// later, read custom data from clipboard
using (var handle = await ClipboardWpf.OpenAsync())
{
    if (handle.ContainsFormat(myCustomTextFormat)) 
    {
        string myCustomText = handle.GetFormatType(myCustomTextFormat);
    }
    if (handle.ContainsFormat(myCustomBinaryFormat)) 
    {
    	byte[] myCustomBytes = handle.GetFormatBytes(myCustomBinaryFormat);  
    }
}

Note: you can use CreateCustomFormat(string formatName) to read data added to the clipboard by other windows applications. You can provide your own IDataConverter<T> if you wish to automatically translate this to a dotnet type using the GetFormatType method, or you can retrieve the data as a byte[] with the GetFormatBytes method.

Clowd.Clipboard.Bitmaps

The purpose of this library is to attempt to create a more robust BMP/DIB parser than currently exists in WPF or in System.Drawing (GDI+). It’s purpose is to provide good interop with packed dibs on the Windows Clipboard, which WPF and GDI do not support.

There are two big barriers to reading Bitmap’s on the clipboard; Both CF_DIB and CF_DIBV5 are available, but they lack BITMAPFILEHEADER’s. Additionally, WPF and GDI do not support the BITMAPV5HEADER present in CF_DIBV5 and therefore can not properly read transparency data. This library has been tested with a variety of weird, rare, invalid bitmap files and supports every bitmap reference file I could come across - including rare reader formats (like OS2v1, OS2v2) and rare compression algorithms like RLE and Huffman1D.

This library may play better with bitmaps written to the clipboard by native applications than GDI or WPF can manage.

Bitmap Known Limitations / Issues

  • Bitmap data will be translated to sRGB if calibration data and/or a embedded ICC color profile exists in the bitmap file. This was done by design, because color profile support in WPF and GDI is limited. This library does not support writing a color profile.
  • Not all pixel formats are supported natively, some are converted to Bgra32 when parsing
  • Does not support Gray colorspace bitmaps

Bitmap Example

There is a convenient to use wrapper for both GDI and WPF, depending on your choice of UI technology.

byte[] imageBytes = File.ReadAllBytes("file.bmp");
BitmapSource bitmap = BitmapWpf.FromBytes(imageBytes, BitmapWpfReaderFlags.PreserveInvalidAlphaChannel);

// profit!
byte[] imageBytes2 = BitmapWpf.ToBytes(bitmap);

Bitmap Compatibility

  • :heavy_check_mark: Files with or without a BITMAPFILEHEADER
  • :heavy_check_mark: Files with any known BMP header format, including WindowsV1, V2, V3, V4, V5, OS2v1, OS2v2
  • :heavy_check_mark: Files with logical color space / calibration data
  • :heavy_check_mark: Files with embedded ICC color formats
  • :heavy_check_mark: Files with any valid compression format, including RGB, BITFIELDS, ALPHABITFIELDS, JPEG, PNG, HUFFMAN1D (G31D), RLE4/8/24
  • :heavy_check_mark: Files with completely non-standard pixel layout (for example, 32bpp with the following layout: 7B-25G-0R-0A)

评论

暂无评论