# How to make a get image by a NETObject

This more then a .net question but I'm in Mathematica using.Suppose I want to get a image from my clipboard,so I make a function like following.If there is a image in your clipboard then you run this code.You will get a image NETObject:

Needs["NETLink"]
InstallNET[];
img=GetImage[]

(*« NETObject[System.Drawing.Bitmap]»*)

How to make the img be a image?There is a related post:

But his image from a Mathemtica expresssion.

• LockBits or something like it might be needed; unfortunately my .NET is not up to snuff. – J. M. will be back soon Dec 11 '16 at 10:41
• @J.M. It's little difficult as my Google. – yode Dec 11 '16 at 11:32
• Here is example code which does what you want in matlab. It is not completely straightforward to translate to Mathematica but a good starting point. Can you explain what exactly you are trying to solve? I could imagine that there might be other possibilities to achieve what you need than using .NET. See e.g. this for an alternative to get a picture from clipboard. – Albert Retey Dec 12 '16 at 9:01
• @AlbertRetey Wow,thanks for your links.Actually I have found that postbefore this,I just want to convert a image object to image in Mathematica. :) – yode Dec 12 '16 at 9:40
• Probably related: mathematica.stackexchange.com/questions/95938/… – Sjoerd C. de Vries Dec 19 '16 at 22:41

I don't know much of .net and it probably shows, but at least I got something to work albeit very,very slowly.

Your code (which I also turned into a bitmap on the clipboard using Mathematica's "Copy as Bitmap" menu item):

InstallNET[];
img = GetImage[]

Using a few of .Net's Image methods to get pixel information:

res = Table[
{
img@GetPixel[r, c]@R,
img@GetPixel[r, c]@G,
img@GetPixel[r, c]@B,
img@GetPixel[r, c]@A
}, {c, 0, img@Height - 1}, {r, 0, img@Width - 1}];

Image[res, "Byte", ColorSpace -> "RGB"]

And after waiting perhaps a few minutes (I said it was slow) with the above mentioned image already loaded in the clipboard you get this:

• Wow,it's seem you make it.Thanks very very much. :) – yode Dec 19 '16 at 23:52
• Feel free for the extra bounties please. :) – yode Mar 8 '17 at 18:01

Done

Needs["NETLink"]; InstallNET[];
"System.Drawing.Rectangle", "System.Drawing.Imaging.ImageLockMode",
"System.Runtime.InteropServices.Marshal"};
img = ClipboardGetImage[];
width = img[Width];
height = img[Height];
lock = img[
LockBits[RectangleFromLTRB[0, 0, width, height],
MarshalCopy[intPtr, byte, 0, totalB];