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.