ImageGear .NET v25.2 - Updated
Developer Guide / How to Work with... / Common Operations / Clipboard Operations
In This Topic
    Clipboard Operations
    In This Topic

    ImageGear .NET allows you to copy, cut, and paste all or part of an image to and from the Windows clipboard. The following sections explain how to:

    Copy or Cut to the Clipboard

    You can copy or cut all or part of an ImGearRasterPage instance to the clipboard:

    Check the Contents of the Clipboard

    You can check whether the clipboard currently contains image data that is usable by ImageGear .NET via the ImGearRasterPage.CanGetFromClipboard property. We recommend you always check that this property returns true before calling ImGearRasterPage.GetFromClipboard.

    Paste from the Clipboard

    To get an image from the clipboard as a new ImGearRasterPage instance, 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 that 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