Skip to content

Fix Clipboard.GetDataObject().GetImage regression for bitmap images#14427

Open
LeafShi1 wants to merge 8 commits intodotnet:mainfrom
LeafShi1:Fix_14423_Clipboard.GetDataObject.GetImage_issue
Open

Fix Clipboard.GetDataObject().GetImage regression for bitmap images#14427
LeafShi1 wants to merge 8 commits intodotnet:mainfrom
LeafShi1:Fix_14423_Clipboard.GetDataObject.GetImage_issue

Conversation

@LeafShi1
Copy link
Copy Markdown
Member

@LeafShi1 LeafShi1 commented Mar 27, 2026

Fixes #14423

Root cause

After PR #11545 moved clipboard to the new typed + NRBF pipeline, bitmap images are stored mainly in the ITypedDataObject typed store, but DataObject.GetImage still only reads from the legacy GetData store, so Clipboard.GetDataObject().GetImage() can no longer see those images on .NET 10/11.

Proposed changes

  • In DataObject.cs:
    Update public virtual Image? GetImage() to:
    First attempt TryGetData<Image>(DataFormats.Bitmap, autoConvert: true, out image) and return that image when available (aligned with Clipboard.GetImage() and the new typed/NRBF pipeline).
    Fall back to GetData(DataFormats.Bitmap, autoConvert: true) as Image to preserve existing behavior for legacy or non-typed data sources.
  • In ClipboardTests.cs:
    Add GetDataObject_GetImage_RoundTripsBitmap:
    Clipboard.SetImage(bitmap) then Clipboard.GetDataObject()DataObject.
    Assert dataObject.GetImage() returns a Bitmap with the expected size and pixel value, and Clipboard.ContainsImage() is true.

Customer Impact

  • After the fix, apps using Clipboard.GetDataObject().GetImage() on .NET 10/11 can copy and paste bitmap images correctly again, just like on .NET 9, with no app changes needed.

Regression?

Risk

  • Minimal

Screenshots

Before

Copy an image to the clipboard, run the code

using System.Windows.Forms;

class Program
{
    [STAThread]
    static void Main()
    {
        dynamic dataObject = Clipboard.GetDataObject();
        var image = dataObject.GetImage();

        if (image == null)
            Console.WriteLine("GetImage() returned null - no image on clipboard.");
        else
            Console.WriteLine($"Got image: {image.GetType().FullName} ({image.Width}x{image.Height})");
    }
}

GetImage() returns null.

After

GetImage() returns a System.Drawing.Bitmap.

Test methodology

  • Unit test

Test environment(s)

  • .net 11.0.0-preview.3.26174.112
Microsoft Reviewers: Open in CodeFlow

@merriemcgaw merriemcgaw requested a review from Shyam-Gupta March 27, 2026 18:22
@Shyam-Gupta
Copy link
Copy Markdown
Member

Requesting @JeremyKuhne for his thoughts.

@JeremyKuhne
Copy link
Copy Markdown
Member

Requesting @JeremyKuhne for his thoughts.

Should follow exactly what the Clipboard class code does, modulo that we know we don't need to fall back to the GetData as we're a known ITypedDataObject. Unfortunate that we missed this, hopefully we've audited the other similar cases.

@JeremyKuhne
Copy link
Copy Markdown
Member

@LeafShi1 I'd recommend ensuring that we have test cases for any similar APIs off DataObject.

…d test that depended on the old GetData fallback.
@LeafShi1
Copy link
Copy Markdown
Member Author

LeafShi1 commented Apr 7, 2026

@LeafShi1 I'd recommend ensuring that we have test cases for any similar APIs off DataObject.

The derived APIs exposed by DataObject each have their own unit tests.

@LeafShi1 LeafShi1 requested a review from Shyam-Gupta April 7, 2026 05:56
@Shyam-Gupta
Copy link
Copy Markdown
Member

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

@Shyam-Gupta
Copy link
Copy Markdown
Member

Product change looks correct now. Only few changes required in test code. Thanks.

…jectTests.cs and adding a "delegation relationship" test for the `GetImage` method.
Shyam-Gupta
Shyam-Gupta previously approved these changes Apr 8, 2026
Copy link
Copy Markdown
Member

@Shyam-Gupta Shyam-Gupta left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Pending tasks are to clean up the test code and fix failing tests.
We will need to backport it to .NET 10 for servicing. Thanks.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a regression where Clipboard.GetDataObject().GetImage() returns null for bitmap images after the move to the typed/NRBF clipboard pipeline by updating DataObject.GetImage() to use the typed retrieval API and adding regression coverage.

Changes:

  • Updated DataObject.GetImage() to retrieve bitmap images via TryGetData<Image>(DataFormats.Bitmap, autoConvert: true, ...).
  • Updated/added unit tests to validate GetImage() uses typed retrieval and to validate clipboard bitmap round-tripping via GetDataObject().GetImage().

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/System.Windows.Forms/System/Windows/Forms/OLE/DataObject.cs Changes GetImage() implementation to use typed TryGetData<Image> for bitmap retrieval.
src/test/unit/System.Windows.Forms/System/Windows/Forms/DataObjectTests.cs Updates GetImage unit test expectations and adds a clipboard round-trip regression test.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

…jectTests.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Regression] Clipboard.GetDataObject().GetImage() returns null on .NET 10

4 participants