PngBitmapEncoder fails on large files

7 posts, 0 answers
  1. Tim
    Tim avatar
    4 posts
    Member since:
    May 2012

    Posted 23 May 2012 Link to this post

    I am using the 2011 Q3 Telerik Silverlight controls.  I'm trying to implement a screenshot functionality by saving the root element of my page to a PNG, using the PngBitmapEncoder class.  When I try to use it on some large images, though, (like the full screen of a large screen resolution) I get an error in the Telerik code.

    Here is the error:

    Index was outside the bounds of the array.
       at Telerik.Windows.Media.Imaging.Zip.DeflateManager.Tally(Int32 dist, Int32 lc)
       at Telerik.Windows.Media.Imaging.Zip.DeflateManager.ProcessSlowDeflate(Int32 flush, Int32& num)
       at Telerik.Windows.Media.Imaging.Zip.DeflateManager.SlowDeflate(Int32 flush)
       at Telerik.Windows.Media.Imaging.Zip.DeflateManager.Deflate(ZipBaseStream strm, Int32 flush)
       at Telerik.Windows.Media.Imaging.Zip.ZipBaseStream.Deflate(Int32 flush)
       at Telerik.Windows.Media.Imaging.Zip.ZipOutputStream.FinishWriting()
       at Telerik.Windows.Media.Imaging.Zip.ZipOutputStream.Close()
       at System.IO.Stream.Dispose()
       at Telerik.Windows.Media.Imaging.PngBitmapEncoder.WriteDataChunks()
       at Telerik.Windows.Media.Imaging.PngBitmapEncoder.Encode(WriteableBitmap image)
       at Telerik.Windows.Media.Imaging.PngBitmapEncoder.Save(Stream stream)
       at Telerik.Windows.Media.Imaging.ImageExporter.Export(FrameworkElement element, Stream stream, BitmapEncoder encoder)
       at Telerik.Windows.Media.Imaging.ExportExtensions.ExportToImage(FrameworkElement element, Stream stream, BitmapEncoder encoder)
       at RadControlsSilverlightApp2.MainPage.myImage_ImageOpened(Object sender, RoutedEventArgs e)

    And here is some example XAML/CS that create the error, using the attached image.

    <UserControl x:Class="RadControlsSilverlightApp2.MainPage"
            mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
        <Grid x:Name="LayoutRoot">
            <Image Name="myImage" Source="cant encode.png" Stretch="None" ImageOpened="myImage_ImageOpened" />
        </Grid>
    </UserControl>

    using System;
    using System.IO;
    using System.Windows;
    using System.Windows.Controls;
    using Telerik.Windows.Media.Imaging;
     
    namespace RadControlsSilverlightApp2
    {
        public partial class MainPage : UserControl
        {
            public MainPage()
            {
                InitializeComponent();
            }
            private void myImage_ImageOpened(object sender, RoutedEventArgs e)
            {
                try
                {
                    ExportExtensions.ExportToImage(myImage, new MemoryStream(), new PngBitmapEncoder());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace);
                }
            }
        }
    }

    Am I doing anything wrong?  Is this simply a bug in the Telerik ZIP/PNG code?
  2. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3299 posts

    Posted 28 May 2012 Link to this post

    Hi Tim,

    I wasn't able to reproduce this issue locally but we have received similar reports indicating that there are types of images that cause such exceptions. This is why I wanted to ask you if you can send us the image you're using to test this code so that we can give it a try on our side and see if it works?

    Thank you in advance.

    All the best,
    Tina Stancheva
    the Telerik team

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

  3. Tim
    Tim avatar
    4 posts
    Member since:
    May 2012

    Posted 28 May 2012 Link to this post

    The image, cant-encode.PNG  was attached to the first post.  Is there another image you're looking for?

    Thank you.
  4. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3299 posts

    Posted 29 May 2012 Link to this post

    Hello Tim,

    I actually tested your code with the image you attached, but I wasn't able to reproduce it and this is why i was wondering if this is the image causing the exceptions on your side. I attached the sample I prepared so that you can have a look at it and let me know if it works for you. Also if you can reproduce the exception in my solution, can you send me the list of steps to follow to reproduce it locally as well?

    Thank you in advance.

    Greetings,
    Tina Stancheva
    the Telerik team

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

  5. Tim
    Tim avatar
    4 posts
    Member since:
    May 2012

    Posted 29 May 2012 Link to this post

    I wasn't able to build your solution as-is for two reasons:
    1. I'm using Silverlight 4.0.50917.0, not Silverlight 5. (Note that the version of the Silverlight client plugin installed, not only the framework version targeted, can affect the behavior of certain parts of Silverlight.)
    2. I'm using the 2011 Q3 build of the Telerik controls (for Silverlight 4 of course, not 5).
    When I converted these two things to my versions, your solution gets the same errors as mine.
    So my "steps to reproduce" are to modify the SilverlightApplication2.csproj file so that:
    1. TargetFrameworkVersion is v4.0
    2. The Telerik references are: 
      <Reference Include="Telerik.Windows.Controls, Version=2011.3.1220.1040, Culture=neutral, PublicKeyToken=5803cfa389c90ce7, processorArchitecture=MSIL" />
      <Reference Include="Telerik.Windows.Zip, Version=2011.3.1220.1040, Culture=neutral, PublicKeyToken=5803cfa389c90ce7, processorArchitecture=MSIL" />
    And:
    1. In SilverlightApplication2TestPage.aspx, change minRuntimeVersion to "4.0".
    2. (not sure if this step is needed) Uninstall Silverlight 5 and install Silverlight 4.0.50917.0.

    Thanks for looking into this.
  6. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3299 posts

    Posted 29 May 2012 Link to this post

    Hi Tim,

    Thank you for getting back to us. I did manage to reproduce the issue in the solution as soon as I changed the targeted version to Silverlight 4 and I used the 2011.3.1220 version of our assemblies. However, when I tested the solution with the 2012.1.326.1040 assemblies (the latest official assemblies targeting Silverlight 4), the exception was gone. Can you try your solution with these assemblies on your side and let me know if there is still an exception? Thank you in advance.

    All the best,
    Tina Stancheva
    the Telerik team

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

  7. Tim
    Tim avatar
    4 posts
    Member since:
    May 2012

    Posted 29 May 2012 Link to this post

    Unless I'm mistaken, my company's support/update license for the Telerik controls expired so that 2011 Q3 is the latest that I can download.  I was able to workaround this for my goal by using the ZipOutputStream and a custom PNG encoder, writing only up to 256k of data at a time between flushing (I found this size via experimentation with my data, I don't know if it depends on any factors, such as compression ratio, but seems to work for me).
Back to Top