Save image generated by PngBitmapEncoder on server

1 posts, 0 answers
  1. Marcelo
    Marcelo avatar
    28 posts
    Member since:
    Jul 2010

    Posted 26 Oct 2011 Link to this post

    Hi Guys,

    We have an app that can save, as a image, its entire page, through this code:


    public void ExportarParaStream(Stream fileStream, string SafeFileName)
                BitmapEncoder encoder = null;
                if (SafeFileName.ToLower().EndsWith(".png"))
                    encoder = new PngBitmapEncoder();
                    encoder = new BmpBitmapEncoder();
                Telerik.Windows.Media.Imaging.ExportExtensions.ExportToImage(PainelSelecionado.View as FrameworkElement, fileStream, encoder);
            public void exportar(object sender)
                SaveFileDialog saveImage = new SaveFileDialog();
                saveImage.Filter = "PNG (*.png)|*.png|Bitmap (*.bmp)|*.bmp";
                if (saveImage.ShowDialog() == true)
                    ExportarParaStream(saveImage.OpenFile(), saveImage.SafeFileName);
    View/PainelSelecionado.View as FrameworkElement:

    <Grid x:Name="LayoutRoot" Background="Transparent">
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="Auto"/>
                    <telerik:RadBusyIndicator [...]/>
            <ScrollViewer [...]>
                    <telerik:RadDocking x:Name="xMainDock" AllowUnsafeMode="True" BorderThickness="0"
                                Grid.Row="0" HasDocumentHost="False"
                <!--- Here we have everithing -->
            <telerik:RadPanelBar [...]>
            <telerik:RadPanelBarItem Header="Propriedades" IsChecked="{x:Null}">
                   <propertyGrid:PropertyGrid OnOcorreuModificacao="PropertyGrid_OnOcorreuModificacao"  SelectedObject="{Binding ObjetoSelecionado, Mode=TwoWay}" Margin="2" MaxWidth="240"/>

    Now, we want to generate this image and send to our server and then save it.
    What is happening is what I'm getting a odd "Parameter is not valid" exception...


            public void SalvarImagem(byte[] stream)
                DirectoryInfo diretorio = new DirectoryInfo(LibGeral.getDiretorio() + "\\Imagens");
                if (!diretorio.Exists)
                int nImagem = 1;
                int nArquivos = diretorio.GetFiles("*.png").Count();
                string imagem = diretorio.FullName + "\\Imagem" + (nImagem + nImagem) + ".png";
                FileInfo file = new FileInfo(imagem);
                while (file.Exists)
                    imagem = diretorio.FullName + "\\Imagem" + (nImagem + nImagem) + ".png";
                    file = new FileInfo(imagem);
                MemoryStream ms = new MemoryStream();
                ms.Write(stream, 0, stream.Length);
                Bitmap bitmap = (Bitmap)Bitmap.FromStream(ms, true); /*---> Exception is throw here!
                //MemoryStream ms = new MemoryStream(stream);
                //Image img = System.Drawing.Image.FromStream(ms);

    I searched and found a lot of possible resolutions and wasted a few hours trying.

    One of the possible resolutions is that the image was "bad formed".
    So, I'm using the PngBitmapEncoder to generate the Stream... I don't need to do the reverse thing?

    You guys doesn't have a better approach? Any ideas? HELP?!

    thanks in advance
Back to Top