I need a help regards copy image to clipboard in website. The image is dynamically created one.
I found two solutions
Solution 1 :
using System.Windows.Forms;
Bitmap bitmapImage ;
protected void buttonClipboard_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
MemoryStream memoryStream = new MemoryStream(imageByteArray); // Image byte array is the input
bitmapImage = (Bitmap)System.Drawing.Image.FromStream(memoryStream);
memoryStream.Flush();
memoryStream.Close();
Thread cbThread = new Thread(new ThreadStart(CopyToClipboard));
cbThread.ApartmentState = ApartmentState.STA;
cbThread.Start();
cbThread.Join();
}
[STAThread]
protected void CopyToClipboard()
{
if (bitmapImage != null)
{
//Clipboard.SetData(DataFormats.Bitmap, bitmapImage);
Clipboard.SetImage(bitmapImage);
}
}
This is working fine before publishing the website.
After publishing this won’t work in any website browsers because here STAThread used.
Some websites said thread won’t work in published website due to internal multi-thread handling in browsers.
Solution 2 :
<script type="text/javascript">
function CopyToClip() {
var imgControl = document.getElementById('imageContent');
imgControl.contentEditable = 'true';
var controlRange;
if (document.body.createControlRange) {
controlRange = document.body.createControlRange();
controlRange.addElement(imgControl);
controlRange.execCommand('Copy');
}
imgControl.contentEditable = 'false';
return true;
}
This java-script working fine for IE before & after publishing website. But it is not working in Chrome & Mozilla in any situation.
I need one solution for Mozilla & Chrome.
Please suggest any solution for this?
IE is the only browser that supports this method of clipboard interaction. No other browser supports placing images in the clipboard.
For text you have to go with a Flash-based solution to achieve cross-browser support. You could try ZeroClipboard.
Mozilla have a clipboard guide but it’s reasonably low level, and judging by this document they are not too keen on opening up the clipboard due to security concerns. In particular on “enabling clipboard access for a website”:
However, there is an interesting W3C API spec for clipboard interaction. In particular, the
setDatamethod seems promising for your use case. This SO answer gives an interesting test of theonpasteevent, but unfortuantly nothing on items on to the clipboard, only retrieving them.