📜 ⬆️ ⬇️

Silverlight copy-paste image from Clipboard

Breaking through a bunch of documentation, I found out that “No we do not have clipboard support at this time. »: ((

I just solved this problem, I hasten to share :)


But it was vital to get and upload a clipboard image to the server, so I began to look for a workaround.
')
As a result, came to a bunch (inspired by the article Google Gears + Silverlight) ActiveX + JS + Silverlight
JavaScript plays the role of a “bridge” between the ActiveX component and Silverlight.

This solution will be used inside the local network, so it is possible to immediately install all ActiveX components using group policy, register the resource in Trusted Sites, etc.
As a solution for the Internet, this method is not so good :)

To begin, write the ActiveX component


public interface IClipboard
{
string Image { get ; }
string Text { get ; set ; }
}

[ClassInterface(ClassInterfaceType.AutoDual)]
public class ClipboardProxy : IClipboard
{

public string Image
{
get
{
Image img = Clipboard.GetImage();
if (img == null )
return null ;

string fileName = Guid .NewGuid().ToString().Split( new char [] { '-' })[0] + ".png" ;

PutFile(fileName, img);

return fileName;
}
}

public string Text
{
get
{
return Clipboard.GetText();
}
set
{
Clipboard.SetText( value );
}
} }


* This source code was highlighted with Source Code Highlighter .


The text from the clipboard can be obtained without using ActiveX, through the object model of the browser, but I did it just in case.

Save the image locally and transfer it to the remote server


private void PutFile( string name, Image img)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create( new Uri ( "http://helpdesk/ServicePages/upload.jsp" ));

request.Credentials = CredentialCache.DefaultNetworkCredentials;

string folder = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache);
folder += "\\ " + name;

img.Save(folder);
FileInfo fileInfo = new FileInfo(folder);

request.PostMultiPartAsync( new Dictionary< string , object > { { "uploadfile" , fileInfo } }, new AsyncCallback(asyncResult =>
{
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult);

Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);

string s = reader.ReadToEnd();

if (response.StatusCode == HttpStatusCode.OK)
{

}

response.Close();

}));

}


* This source code was highlighted with Source Code Highlighter .


Do not forget to set up COM Assembly in the Make Assembly library settings (Project properties - Application - Assembly Information)
and register it with the command:
regasm < >.dll /tlb /codebase

In the code, the helper is used to send a multipart form, where I didn’t find the link :(

We write function bridge


< script language ="javascript" >
<!-- Load the ActiveX object -->
var xClipboard = new ActiveXObject( "Megafon.ClipboardHelper.ClipboardProxy" );

function GetClipboardImage()
{

return xClipboard.Image;
}

</ script >


* This source code was highlighted with Source Code Highlighter .


The function returns the file name that we uploaded to the server using an ActiveX component.
there was a thought to transmit byte [] but I did not overcome it ... JScript fell with an error :(
Then you can not send the picture to the server, but use them immediately inside the silver light.
For my task, I just needed to send a screenshot to the server, so this solution came up to me.

The last step is a call from silverlight, I hung up the handler on the F12 button in the description input window


private void tbxDescription_KeyDown( object sender, System.Windows.Input.KeyEventArgs e)
{

if (e.Key == (Key.F12))
{
this .Dispatcher.BeginInvoke(( delegate ()
{
object obj = HtmlPage.Window.Eval( "(function(){ return GetClipboardImage(); })()" );

if (obj != null )
{
_attach.Add( new AttachItem() { FileName = obj.ToString() });
}

}));
}

}


* This source code was highlighted with Source Code Highlighter .


Further, the script to which silverlight will send these forms contains only file names.
The files themselves are already on the server;)

Thank you for your attention, I hope that this technique will be useful to someone :)

PS: you can get the text content of the clipboard like this:
HtmlPage.Window.Eval( "window.clipboardData.getData('Text')" );

* This source code was highlighted with Source Code Highlighter .

Source: https://habr.com/ru/post/65748/


All Articles