Get a Clipboard picture.



  • In an attempt to retrieve the Clipboard, it's empty. What's the problem? Or can we copy a picture of richTextBox by blowing Clipboard?

    Clipboard.Clear();
    if ((this.richTextBox5.SelectionType & RichTextBoxSelectionTypes.Object) == RichTextBoxSelectionTypes.Object)
    {
        richTextBox5.Copy();
        if (Clipboard.ContainsImage())
        {
            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox1.Image = Clipboard.GetImage();
        }
        else
        {
            MessageBox.Show("Пусто");
        }
    


  • Method Clipboard.ContainsImage() Check that there is a BMP image exchange in the buffer. When copying RichText, there's likely to be an EMF image. It can be verified by a challenge Clipboard.ContainsData(DataFormats.EnhancedMetafile)

    The image must now be removed from the exchange buffer. However, for class Clipboard EMF extraction for some reason is not working: https://stackoverflow.com/questions/19868816/

    Therefore, it will be necessary to write an additional code to address the Native WinAPI to extract the image from the buffer:

    private const int CF_ENHMETAFILE = 14;
    

    [DllImport("user32.dll")]
    private static extern bool OpenClipboard(IntPtr hWndNewOwner);

    [DllImport("user32.dll")]
    private static extern int IsClipboardFormatAvailable(int wFormat);

    [DllImport("user32.dll")]
    private static extern IntPtr GetClipboardData(int wFormat);

    [DllImport("user32.dll")]
    private static extern int CloseClipboard();

    private Image GetImage()
    {
    Metafile result = null;
    if (OpenClipboard(this.Handle)) {
    try {
    if (IsClipboardFormatAvailable(CF_ENHMETAFILE) != 0) {
    var hMeta = GetClipboardData(CF_ENHMETAFILE);
    if (hMeta != IntPtr.Zero)
    result = new Metafile(hMeta, true);
    }
    } finally {
    CloseClipboard();
    }
    }

    return result;
    

    }

    The exchange booth can now be operated:

    Clipboard.Clear();

    if ((richTextBox5.SelectionType & RichTextBoxSelectionTypes.Object) != 0) {
    richTextBox5.Copy();
    if (Clipboard.ContainsData(DataFormats.EnhancedMetafile)) {
    var image = GetImage();
    if (image != null)
    pictureBox1.Image = image;
    }
    }




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2