Skip to content

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

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

Fix Clipboard.GetDataObject().GetImage regression for bitmap images#14427
LeafShi1 wants to merge 3 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
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

3 participants