PNG images not always displaying properly

4 posts, 1 answers
  1. Frank
    Frank avatar
    4 posts
    Member since:
    Jul 2011

    Posted 09 Apr 2012 Link to this post

    Hello,

    My development team is currently evaluating the trial version of the RichTextBox to find out whether or not it will meet our current application requirements, one of which is pasting images.  Our users often take screenshots using an application called SnagIt and they need to be able to paste those screenshots into the editor.  

    We are encountering some issues when pasting PNG images, but the behavior varies depending on the source of the image.  If we use Microsoft's Snipping Tool to take the screenshot, it pastes into the editor successfully.  However, if we use SnagIt, or even open an image in Microsoft Paint and then try to copy and paste it, the cursor in the textbox moves as if an image was pasted, but no image is displayed.  The area where the image should be is a white box.

    We have done no customization of the RichTextBox or anything.  All I did was simply drag the control from the Visual Studio Toolbox onto the WPF canvas.

    I have not seen any other posts where anyone has encountered this same issue.  I'm hoping someone has perhaps noticed this same issue before, or to bring it to the attention of the developers as a potential bug that needs to be fixed.  I've attached an image showing how the editor looks exhibiting successful pasting behavior and the unsuccessful behavior.

    Thanks,

    Frank
  2. Answer
    Iva Toteva
    Admin
    Iva Toteva avatar
    1319 posts

    Posted 09 Apr 2012 Link to this post

    Hello Frank,

    Thank you for getting to us about this issue.

    Similar issues have been reported when pasting images from MS Paint. When we investigated the problem, it turned out to be a common problem with corrupted data being saved in the clipboard from some programs. There is more information about this issue here.

    Unfortunately, the workaround which can be implemented based on the description and code in the blog, is to save all images to a memory stream first. As this may hinder the overall performance, we are refraining from implementing the workaround in the editor. The good news is that you can workaround it on your end quite easily. All you need to do is to handle the CommandExecuting event of RadRichTextBox like this:

    void editor_CommandExecuting(object sender, Telerik.Windows.Documents.RichTextBoxCommands.CommandExecutingEventArgs e)
    {
        PasteCommand command = e.Command as PasteCommand;
        if (command != null && Clipboard.ContainsImage())
        {
            BitmapSource imageSource = Clipboard.GetImage();
            using (MemoryStream stream = new MemoryStream())
            {
                JpegBitmapEncoder encoder = new JpegBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(imageSource));
                encoder.Save(stream);
                stream.Seek(0, SeekOrigin.Begin);
                ((RadRichTextBox)sender).InsertImage(stream, "jpeg");
                e.Cancel = true;
            }
        }
    }

    You can also track this PITS issue item in order to be notified when the workaround is implemented in the editor, making this code redundant.

    I hope this helps.

    Regards,
    Iva Toteva
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. UI for WPF is Visual Studio 2017 Ready
  4. Frank
    Frank avatar
    4 posts
    Member since:
    Jul 2011

    Posted 09 Apr 2012 Link to this post

    Iva,

    We had already figured out the work-around, but it seems like the images lose a bit of quality when converted to JPEG.  We were hoping for a better solution, but we definitely appreciate the response.  I will track the PITS issue item you provided a link to.

    Thanks,

    Frank
  5. Robert
    Robert avatar
    271 posts
    Member since:
    Mar 2011

    Posted 16 Nov 2012 Link to this post

    I've also noticed quality issues with PNG images.
    I will have to try a different format.
Back to Top