This is a migrated thread and some comments may be shown as answers.

PngBitmapEncoder fails on large files

6 Answers 219 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Tim
Top achievements
Rank 1
Tim asked on 23 May 2012, 05:23 PM
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?

6 Answers, 1 is accepted

Sort by
0
Tina Stancheva
Telerik team
answered on 28 May 2012, 05:11 PM
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 >>

0
Tim
Top achievements
Rank 1
answered on 28 May 2012, 07:26 PM
The image, cant-encode.PNG  was attached to the first post.  Is there another image you're looking for?

Thank you.
0
Tina Stancheva
Telerik team
answered on 29 May 2012, 09:33 AM
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 >>

0
Tim
Top achievements
Rank 1
answered on 29 May 2012, 02:39 PM
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.
0
Tina Stancheva
Telerik team
answered on 29 May 2012, 06:17 PM
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 >>

0
Tim
Top achievements
Rank 1
answered on 29 May 2012, 09:39 PM
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).
Tags
General Discussions
Asked by
Tim
Top achievements
Rank 1
Answers by
Tina Stancheva
Telerik team
Tim
Top achievements
Rank 1
Share this question
or