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:

    VM:

         
    public void ExportarParaStream(Stream fileStream, string SafeFileName)
            {
                BitmapEncoder encoder = null;
                if (SafeFileName.ToLower().EndsWith(".png"))
                {
                    encoder = new PngBitmapEncoder();
                }
                else
                {
                    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">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>
                    <telerik:RadBusyIndicator [...]/>
            <ScrollViewer [...]>
                    <telerik:RadDocking x:Name="xMainDock" AllowUnsafeMode="True" BorderThickness="0"
                                LayoutChangeEnded="xMainDock_LayoutChangeEnded"
                                Grid.Row="0" HasDocumentHost="False"
                                PreviewClose="xMainDock_PreviewClose">
                <!--- Here we have everithing -->
                    </telerik:RadDocking>
            </ScrollViewer>
            <telerik:RadPanelBar [...]>
            <telerik:RadPanelBarItem Header="Propriedades" IsChecked="{x:Null}">
                   <propertyGrid:PropertyGrid OnOcorreuModificacao="PropertyGrid_OnOcorreuModificacao"  SelectedObject="{Binding ObjetoSelecionado, Mode=TwoWay}" Margin="2" MaxWidth="240"/>
                        </telerik:RadPanelBarItem>
                    </telerik:RadPanelBar>
        </Grid>

    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...

    Server:

    [Invoke]
            [RequiresAuthentication]
            public void SalvarImagem(byte[] stream)
            {
                DirectoryInfo diretorio = new DirectoryInfo(LibGeral.getDiretorio() + "\\Imagens");
     
                if (!diretorio.Exists)
                    diretorio.Create();
     
                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)
                {
                    nImagem++;
                    imagem = diretorio.FullName + "\\Imagem" + (nImagem + nImagem) + ".png";
                    file = new FileInfo(imagem);
                }
     
                MemoryStream ms = new MemoryStream();
     
                ms.Write(stream, 0, stream.Length);
                ms.Close();
                Bitmap bitmap = (Bitmap)Bitmap.FromStream(ms, true); /*---> Exception is throw here!
     
                //MemoryStream ms = new MemoryStream(stream);
     
                //Image img = System.Drawing.Image.FromStream(ms);
     
                //img.Save(imagem);
                //ms.Close();
                 
            }


    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