Skip to main content
added 695 characters in body
Source Link
Jerry
  • 2.5k
  • 10
  • 17

... What to do is to follow the .NET rule to make it.

loadImage[plot_] :=
    Module[ {result},
        (*produce file*)
        file = CreateTemporary[];
        Export[file, plot, "GIF"];
            
        (*get object*)
        LoadNETType["System.Drawing.Image"];
        result = Image`FromFile[file];
            
        (*clean*)
        DeleteFile[file];(*may catch Privilege problem*)
        (*return as object*)
        Return[result];
    ]
    

then pass the object produced to the property.

p1 = Plot[Sin[x], {x, -4, 4}];

Needs["NETLink`"];
InstallNET[];

LoadNETType["System.Windows.Forms.ImageLayout"];

frm = NETNew["System.Windows.Forms.Form"];
frm@Width = 500;
frm@Height = 300;
frm@BackgroundImageLayout = ImageLayout`Stretch;
frm@BackgroundImage = loadImage[p1]; (*now ok*)

ShowNETWindow[frm]

[![enter image description here][1]][1]enter image description here

However, I don't know would it be efficient on large projects. [1]

Update: use https://i.sstatic.net/oa07R.pngFileStream to avoid priviledge problem.

loadImage2[plot_] :=
 NETBlock@Module[{fs, result},
   (*produce file*)
   file = CreateTemporary[];
   Export[file, plot, "GIF"];
   
   (*get object*)
   LoadNETType["System.Drawing.Image"];
   LoadNETType["System.IO.FileMode"];
   LoadNETType["System.IO.FileAccess"];
   
   fs = NETNew["System.IO.FileStream", file, FileMode`Open, 
     FileAccess`Read];
   result = Image`FromStream[fs];
   
   (*clean*)
   fs@Close[];
   DeleteFile[file];
   (*return as object*)
   Return[result];
   ]

... What to do is to follow the .NET rule to make it.

loadImage[plot_] :=
    Module[ {result},
        (*produce file*)
        file = CreateTemporary[];
        Export[file, plot, "GIF"];
            
        (*get object*)
        LoadNETType["System.Drawing.Image"];
        result = Image`FromFile[file];
            
        (*clean*)
        DeleteFile[file];(*may catch Privilege problem*)
        (*return as object*)
        Return[result];
    ]
    

then pass the object produced to the property.

p1 = Plot[Sin[x], {x, -4, 4}];

Needs["NETLink`"];
InstallNET[];

LoadNETType["System.Windows.Forms.ImageLayout"];

frm = NETNew["System.Windows.Forms.Form"];
frm@Width = 500;
frm@Height = 300;
frm@BackgroundImageLayout = ImageLayout`Stretch;
frm@BackgroundImage = loadImage[p1]; (*now ok*)

ShowNETWindow[frm]

[![enter image description here][1]][1]

However, I don't know would it be efficient on large projects. [1]: https://i.sstatic.net/oa07R.png

... What to do is to follow the .NET rule to make it.

loadImage[plot_] :=
    Module[ {result},
        (*produce file*)
        file = CreateTemporary[];
        Export[file, plot, "GIF"];
            
        (*get object*)
        LoadNETType["System.Drawing.Image"];
        result = Image`FromFile[file];
            
        (*clean*)
        DeleteFile[file];(*may catch Privilege problem*)
        (*return as object*)
        Return[result];
    ]
    

then pass the object produced to the property.

p1 = Plot[Sin[x], {x, -4, 4}];

Needs["NETLink`"];
InstallNET[];

LoadNETType["System.Windows.Forms.ImageLayout"];

frm = NETNew["System.Windows.Forms.Form"];
frm@Width = 500;
frm@Height = 300;
frm@BackgroundImageLayout = ImageLayout`Stretch;
frm@BackgroundImage = loadImage[p1]; (*now ok*)

ShowNETWindow[frm]

enter image description here

However, I don't know would it be efficient on large projects.

Update: use FileStream to avoid priviledge problem.

loadImage2[plot_] :=
 NETBlock@Module[{fs, result},
   (*produce file*)
   file = CreateTemporary[];
   Export[file, plot, "GIF"];
   
   (*get object*)
   LoadNETType["System.Drawing.Image"];
   LoadNETType["System.IO.FileMode"];
   LoadNETType["System.IO.FileAccess"];
   
   fs = NETNew["System.IO.FileStream", file, FileMode`Open, 
     FileAccess`Read];
   result = Image`FromStream[fs];
   
   (*clean*)
   fs@Close[];
   DeleteFile[file];
   (*return as object*)
   Return[result];
   ]
Source Link
Jerry
  • 2.5k
  • 10
  • 17

... What to do is to follow the .NET rule to make it.

loadImage[plot_] :=
    Module[ {result},
        (*produce file*)
        file = CreateTemporary[];
        Export[file, plot, "GIF"];
            
        (*get object*)
        LoadNETType["System.Drawing.Image"];
        result = Image`FromFile[file];
            
        (*clean*)
        DeleteFile[file];(*may catch Privilege problem*)
        (*return as object*)
        Return[result];
    ]
    

then pass the object produced to the property.

p1 = Plot[Sin[x], {x, -4, 4}];

Needs["NETLink`"];
InstallNET[];

LoadNETType["System.Windows.Forms.ImageLayout"];

frm = NETNew["System.Windows.Forms.Form"];
frm@Width = 500;
frm@Height = 300;
frm@BackgroundImageLayout = ImageLayout`Stretch;
frm@BackgroundImage = loadImage[p1]; (*now ok*)

ShowNETWindow[frm]

[![enter image description here][1]][1]

However, I don't know would it be efficient on large projects. [1]: https://i.sstatic.net/oa07R.png