'Clipboard' does not exist in the namespace 'System.Windows.Forms'

I have imported the namespace System.Windows.Forms and also added the
<AssemblyReference>System.Windows.Forms</AssemblyReference> still getting the error as

error CS0234: The type or namespace name ‘Clipboard’ does not exist in the namespace ‘System.Windows.Forms’ (are you missing an assembly reference?

Trying the method:-
System.Windows.Forms.Clipboard.GetImage()

@Sanjit_Pal

You have a clipboard in system.windows as well can you add that and tey

Cheers

System.Drawing.Image image = System.Windows.Clipboard.GetImage();

I tried this way as well getting compiler errors as
Cannot implicitly convert type ‘System.Windows.Media.Imaging.BitmapSource’ to ‘System.Drawing.Image’

I went through this thread but no luck :frowning:

here we would recommend to share

  • what was done
  • what was working / not working

we cannot derive anything from this feedback

Test.zip (2.3 KB)

What I am trying to do is to copy the excel range as a picture and then save it as an image. PFA the .xaml for reference.

as the compiler is clear telling a different datatype is returned:


taken from: Clipboard.GetImage Method (System.Windows) | Microsoft Learn

So a change to:
grafik

and importing: System.Windows.Media.Imaging to the namespaces (Imports panel, clos to the variables panel) will solve so far.

A success we can check with:
grafik

As this datatype does not offer a save() method, we also have to handle and modify this code part as well.

Quick dirty for the first start:
grafik

//Conversion
BmpBitmapEncoder encoder = new BmpBitmapEncoder();
MemoryStream ms = new MemoryStream();
encoder.Frames.Add(BitmapFrame.Create((BitmapSource) image));
encoder.Save(ms);
System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
//Saving
img.Save("YourPathToABMPFile",  System.Drawing.Imaging.ImageFormat.Bmp );