ImageGear for .NET
Pasting an Image from the Clipboard

To get an image from the clipboard as a new ImGearRasterPage instance, simply call the static ImGearRasterPage.GetFromClipboard method. If the clipboard contains valid image data, a new ImGearRasterPage instance will be returned.

If the clipboard contains data previously copied from an ImGearRasterPage instance, which happened to contain a non-rectangular region of interest, then the new ImGearRasterPage instance returned from the clipboard will contain a matching non-rectangular region of interest (the ImGearRasterPage.ROI property will contain an ImGearROIMask instance that matches the mask originally copied to the clipboard).

ImageGear .NET does not provide a special method for pasting the clipboard image into a pre-existing ImGearRasterPage. However, this can be easily achieved using the ImGearRasterProcessing.Merge method.

The following example includes code to handle mismatched color spaces and channel depths:

C# Example
Copy Code
ImGearRasterPage page = ImGearRasterPage.GetFromClipboard();
if (page != null)
{
    ImGearRasterPage currentPage = this.imGearPage as ImGearRasterPage;
    if (currentPage != null)
    {
        if (!currentPage.DIB.ColorSpace.Equals(page.DIB.ColorSpace))
        {
            // Convert to the destination color space.
            ImGearRasterProcessing.ConvertColorSpace(page, currentPage.DIB.ColorSpace);
        }
        bool changeDepth = false;
        if (currentPage.DIB.ChannelDepths.Length != page.DIB.ChannelDepths.Length)
        {
            changeDepth = true;
        }
        else
        {
            for (int i = 0; i < currentPage.DIB.ChannelDepths.Length; ++i)
            {
                if (currentPage.DIB.ChannelDepths[i] != page.DIB.ChannelDepths[i])
                {
                    changeDepth = true;
                    break;
                }
            }
        }
        if (changeDepth)
        {
            // Convert to the destination bit depth.
            ImGearRasterProcessing.ChangeChannelDepths(page, currentPage.DIB.ChannelDepths);
        }
        if (ImGearRasterProcessing.Verifier.CanApplyMerge(currentPage, page))
        {
            ImGearRasterProcessing.Merge(currentPage, page, 0, 0, ImGearMergeModes.OVER);
        }
        else
        {
            // Incompatible images. Just replace the current page by page from clipboard.
            this.imGearPage = page;
        }
    }
    else
    {
        this.imGearPage = page;
    }
}

 

VB.NET Example
Copy Code
Dim page As ImGearRasterPage = ImGearRasterPage.GetFromClipboard()
If page IsNot Nothing Then
Dim currentPage As ImGearRasterPage = TryCast(Me.imGearPage, ImGearRasterPage)
    If currentPage IsNot Nothing Then
        If Not currentPage.DIB.ColorSpace.Equals(page.DIB.ColorSpace) Then
' Convert to the destination color space.
            ImGearRasterProcessing.ConvertColorSpace(page, currentPage.DIB.ColorSpace)
        End If
Dim changeDepth As Boolean = False
        If currentPage.DIB.ChannelDepths.Length <> page.DIB.ChannelDepths.Length Then
            changeDepth = True
        Else
            For i As Integer = 0 To currentPage.DIB.ChannelDepths.Length - 1
                If currentPage.DIB.ChannelDepths(i) <> page.DIB.ChannelDepths(i) Then
                    changeDepth = True
                    Exit For
                End If
            Next
        End If
        If changeDepth Then
' Convert to the destination bit depth.
            ImGearRasterProcessing.ChangeChannelDepths(page, currentPage.DIB.ChannelDepths)
        End If
        If ImGearRasterProcessing.Verifier.CanApplyMerge(currentPage, page) Then
            ImGearRasterProcessing.Merge(currentPage, page, 0, 0, ImGearMergeModes.OVER)
        Else
' Incompatible images. Just replace the current page by page from clipboard.
            Me.imGearPage = page
        End If
    Else
        Me.imGearPage = page
    End If
End If

 

 


©2014. Accusoft Corporation. All Rights Reserved.

Send Feedback