1

I'm working with an API that produces images in BMP or JPG format. The API method in question requires a path and filename. However, in some cases, I would like to simply put the image into the Clipboard.

I know I can save to a file, then open the file and copy the Stream's data to the Clipboard:

private void SaveSnapshot(string path)
    {
        var pathWasblank = path.IsNullOrBlank();
        path = path.NullIfBlank() ?? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures),"tempFile.jpg");
        myApiInstance.ExportJPG(path, 100, true);

        //to ensure the file has made it to the filesystem
        while(!File.Exists(path)) Thread.Sleep(100);

        if (pathWasblank) //it should be on the Clipboard and not in a file
        {
            using (var stream = File.OpenRead(path))
                Clipboard.SetImage(Image.FromStream(stream));

            File.Delete(path); //cleanup;
        }
    }

What I want to know is if there is a way to skip having to reopen, copy and then delete the file, and instead "save" directly to the Clipboard by specifying some file path that corresponds to the Clipboard. If not, no big deal; I have a working solution.

KeithS
  • 70,210
  • 21
  • 112
  • 164
  • 2
    It depends on how this API produce images. If you can supply the stream, you can pass it a MemoryStream. If it returns a byte[] you can create a MemoryStream from that array. If all it accepts is a path to a file, there is nothing you can do to change it. – Tergiver May 01 '12 at 16:51
  • That's what I was afraid of. Yeah, if I could get image data from this API as a Stream or byte array it would solve SO many other problems, but no such luck. I was hoping there was some "shortcut" path that would put data saved to it in the Clipboard, or something similar, but apparently not. – KeithS May 01 '12 at 16:56

0 Answers0