Kopieren Sie das Bild in die Zwischenablage und lassen Sie es als Datei eingefügt (VB.NET)
Frage
Ich habe ein Bildbox und wenn ich unten Snipet verwende:
Clipboard.SetImage(PictureBox.image)
Dann kann ich das Bild nur in Dinge wie Farbe und MS -Wort einfügen. Ich kann es nicht als Datei in einen Ordner/einen Desktop einfügen.
Wie kann ich das Bild in die Zwischenablage kopieren und wenn es in einen Ordner eingefügt wird, wird es zu einer Datei?
Lösung
If you're using .net and your ultimate goal is to save the file, there's a LOT easier way,
Here the code in C#, porting it into VB.net won't be hard, I'm just too lazy to do that :) Anyway, you do have to save it somewhere before you can paste it so...
It loads the file to the Picture box and again saves it to a file, (lame, I know) and set the clipboard data as a copy operation
then when you paste (Ctrl+V) it, it gets pasted.
C#
__
Bitmap bmp;
string fileName=@"C:\image.bmp";
//here I assume you load it from a file, you might get the image from somewhere else, your code may differ
pictureBox1.Image=(Image) Bitmap.FromFile(fileName);
bmp=(Bitmap)pictureBox1.Image;
bmp.Save(@"c:\image2.bmp");
System.Collections.Specialized.StringCollection st = new
System.Collections.Specialized.StringCollection();
st.Add(@"c:\image2.bmp");
System.Windows.Forms.Clipboard.SetFileDropList(st);
</pre>
and viola tries pasting in a folder the file image2.bmp will be pasted.
Andere Tipps
Here's basically what @Vivek posted but ported to VB. Up-vote his if this works for you. What you have to understand is that explorer will only allow you to paste files, not objects (AFAIK anyway). The reason is because if you copy image data to the clipboard, what format should it paste in? PNG, BMP, JPG? What compression settings? So like @Vivek said, you need to think those over, create a file on your own somewhere on the system and use SetFileDropList
which will add the temp file to the clipboard.
' Add it as an image
Clipboard.SetImage(PictureBox1.Image)
'Create a JPG on disk and add the location to the clipboard
Dim TempName As String = "TempName.jpg"
Dim TempPath As String = System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Temp, TempName)
Using FS As New System.IO.FileStream(TempPath, IO.FileMode.Create, IO.FileAccess.Write, IO.FileShare.Read)
PictureBox1.Image.Save(FS, System.Drawing.Imaging.ImageFormat.Jpeg)
End Using
Dim Paths As New System.Collections.Specialized.StringCollection()
Paths.Add(TempPath)
Clipboard.SetFileDropList(Paths)