Bret Pehrson
12/2/2004 8:58:00 PM
I have the following requirement: copy a graph image to the clipboard. The
graph control supports a SaveAsImage method that will copy the image in a
metafile format to the specified stream.
If I use a file stream, everything works as expected, however I have to persist
to file, read back in, and then copy to the clipboard, which seems pretty
backwards to me.
private void CopyViaFileStream()
{
string tempFile = Path.GetTempFileName();
FileStream stream = new FileStream(tempFile, FileMode.Create);
graph.SaveAsImage(stream, ImageFormat.Emf);
stream.Close();
Metafile mf = new Metafile(tempFile);
ClipboardMetafileHelper.PutEnhMetafileOnClipboard(this.Handle, mf);
File.Delete(tempFile);
}
However, if I use a memory stream, it dies on the construction of the metafile
from the memory stream w/ a "A generic error occurred in GDI+" exception:
private void CopyViaMemoryStream()
{
MemoryStream stream = new MemoryStream();
graph.SaveAsImage(stream, ImageFormat.Emf);
Metafile mf = new Metafile(stream); // A generic error occurred in GDI+
ClipboardMetafileHelper.PutEnhMetafileOnClipboard(this.Handle, mf);
}
Anyone have any comments on how to get the via memory stream method to work?
(The ClipboardMetafileHelper.PutEnhMetafileOnClipboard method converts from the
(incompatible!!!) .Net metafile format to the traditional (supported) metafile
format.)