2

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?

Krishna
  • 4,892
  • 18
  • 63
  • 98

2 Answers2

0

It doesn't really work in Firefox without the user hand editing at least one config file.

In chrome it's disabled except for extensions.

If you want cross-browser clipboard support sadly a flash widget is the only truly viable way for now.

tkone
  • 22,092
  • 5
  • 54
  • 78
0

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":

There is no such functionality built in to Firefox.

However, there is an interesting W3C API spec for clipboard interaction. In particular, the setData method seems promising for your use case. This SO answer gives an interesting test of the onpaste event, but unfortuantly nothing on items on to the clipboard, only retrieving them.

Community
  • 1
  • 1
Alex
  • 9,313
  • 1
  • 39
  • 44