private static bool CopyImageAlternative2(Image img, string fileName = null) { using (Bitmap bmpNonTransparent = img.CreateEmptyBitmap(PixelFormat.Format24bppRgb)) using (MemoryStream msPNG = new MemoryStream()) using (MemoryStream msDIB = new MemoryStream()) { IDataObject dataObject = new DataObject(); using (Graphics g = Graphics.FromImage(bmpNonTransparent)) { g.Clear(Color.White); g.DrawImage(img, 0, 0, img.Width, img.Height); } dataObject.SetData(DataFormats.Bitmap, true, bmpNonTransparent); img.Save(msPNG, ImageFormat.Png); dataObject.SetData(FORMAT_PNG, false, msPNG); byte[] dibData = ClipboardHelpersEx.ConvertToDib(img); msDIB.Write(dibData, 0, dibData.Length); dataObject.SetData(DataFormats.Dib, false, msDIB); if (!string.IsNullOrEmpty(fileName)) { string htmlFragment = GenerateHTMLFragment($"<img src=\"{fileName}\"/>"); dataObject.SetData(DataFormats.Html, htmlFragment); } return(CopyData(dataObject)); } }
private static Bitmap GetImageAlternative2() { IDataObject dataObject = Clipboard.GetDataObject(); if (dataObject != null) { string[] dataFormats = dataObject.GetFormats(false); if (dataFormats.Contains(FORMAT_PNG)) { using (MemoryStream ms = dataObject.GetData(FORMAT_PNG) as MemoryStream) { if (ms != null) { using (Bitmap bmp = new Bitmap(ms)) { return(ClipboardHelpersEx.CloneImage(bmp)); } } } } else if (dataFormats.Contains(DataFormats.Dib)) { using (MemoryStream ms = dataObject.GetData(DataFormats.Dib) as MemoryStream) { if (ms != null) { return(ClipboardHelpersEx.ImageFromClipboardDib(ms.ToArray())); } } } else if (dataFormats.Contains(FORMAT_17)) { using (MemoryStream ms = dataObject.GetData(FORMAT_17) as MemoryStream) { if (ms != null) { return(ClipboardHelpersEx.DIBV5ToBitmap(ms.ToArray())); } } } else if (dataFormats.Contains(DataFormats.Bitmap)) { return(dataObject.GetData(DataFormats.Bitmap, true) as Bitmap); } } return(null); }
private void UpdateSelectedClipboardContent() { ResetSelected(); if (lvClipboardContentList.SelectedItems.Count > 0) { ListViewItem lvi = lvClipboardContentList.SelectedItems[0]; string format = lvi.Text; if (CurrentDataObject != null) { object data = CurrentDataObject.GetData(format); if (data == null) { RefreshClipboardContentList(); } else { try { switch (data) { case MemoryStream ms: if (format.Equals(ClipboardHelpers.FORMAT_PNG, StringComparison.OrdinalIgnoreCase)) { using (Bitmap bmp = new Bitmap(ms)) { Bitmap clonedImage = ClipboardHelpersEx.CloneImage(bmp); LoadImage(clonedImage); } } else if (format.Equals(DataFormats.Dib, StringComparison.OrdinalIgnoreCase)) { Bitmap bmp = ClipboardHelpersEx.ImageFromClipboardDib(ms.ToArray()); LoadImage(bmp); } else if (format.Equals(ClipboardHelpers.FORMAT_17, StringComparison.OrdinalIgnoreCase)) { Bitmap bmp = ClipboardHelpersEx.DIBV5ToBitmap(ms.ToArray()); LoadImage(bmp); } else { LoadText(data.ToString()); } break; case Bitmap bmp: LoadImage(bmp); break; default: LoadText(data.ToString()); break; } } catch (Exception e) { e.ShowError(); } } } } }