复制或剪切文件到剪切板
/// <summary> /// 复制或剪切文件到剪切板 /// </summary> /// <param name="files">文件路径数组</param> /// <param name="cut">true:剪切;false:复制</param> public static void CopyToClipboard(string[] files, bool cut) { if (files == null) return; IDataObject data = new DataObject(DataFormats.FileDrop, files); MemoryStream memo = new MemoryStream(4); byte[] bytes = new byte[] { (byte)(cut?2:5),0,0,0}; memo.Write(bytes,0,bytes.Length); data.SetData("PreferredDropEffect",memo); Clipboard.SetDataObject(data,false); }
复制文本
private void La_MouseRightButtonDown(object sender, MouseButtonEventArgs e) { StackPanel st = e.Source as StackPanel; StringBuilder Copystr = new StringBuilder(); foreach (TextBox tb in GetChildObjects<TextBox>(st)) { Copystr.Append(" "); Copystr.Append(tb.Text); SolidColorBrush myBrush = new SolidColorBrush(System.Windows.Media.Color.FromArgb(0xFF, 0xad, 0xd6, 0xff)); tb.Background = myBrush; } Clipboard.SetText(Copystr.ToString()); MessageToolTip.Show("数据复制成功"); }
保存图片
// UI --> Png public static void UISaveToPng(this FrameworkElement ui, string fileName) { int width = (int)ui.ActualWidth; int height = (int)ui.ActualHeight; RenderTargetBitmap bmp = new RenderTargetBitmap(width, height, 96d, 96d, PixelFormats.Pbgra32); bmp.Render(ui); PngBitmapEncoder encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(bmp)); FileStream fs = new FileStream(fileName, FileMode.Create); encoder.Save(fs); fs.Close(); }
转BitmapImage文章来源:https://www.toymoban.com/news/detail-600070.html
int width = (int)grid2DMap.ActualWidth; int height = (int)grid2DMap.ActualHeight; RenderTargetBitmap bmp = new RenderTargetBitmap(width, height, 96d, 96d, PixelFormats.Pbgra32); bmp.Render(grid2DMap); PngBitmapEncoder encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(bmp)); BitmapImage bitmapImage = new BitmapImage(); using (var memoryStream = new MemoryStream()) { encoder.Save(memoryStream); memoryStream.Seek(0, SeekOrigin.Begin); bitmapImage.BeginInit(); bitmapImage.CacheOption = BitmapCacheOption.OnLoad; bitmapImage.StreamSource = memoryStream; bitmapImage.EndInit(); }
转System.Drawing.Image文章来源地址https://www.toymoban.com/news/detail-600070.html
MemoryStream memoryStream = new MemoryStream(); encoder.Save(memoryStream); System.Drawing.Image img = System.Drawing.Image.FromStream(memoryStream);
到了这里,关于WPF 复制粘贴到系统粘贴板的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!