2009-06-04 8 views
1

J'essaie d'enregistrer un contrôle wpf dans un fichier, mais j'applique un effet PixelShader, et lorsque j'essaie de sauvegarder, l'image enregistrée est entièrement blanche , noir ou rouge ... dépend des paramètres de l'effet.Enregistrement d'un visuel dans un fichier avec effet PixelShader - WPF

J'utilise le code ici: WPF - Programmatic Binding on a BitmapEffect

comment puis-je enregistrer correctement, il?

merci!

MISE À JOUR: le code que je utilise est:

 BitmapSource bitmap = preview.Source as BitmapImage; 
     Rectangle r = new Rectangle(); 
     r.Fill = new ImageBrush(bitmap); 
     r.Effect = effect; 
     Size sz = new Size(bitmap.PixelWidth, bitmap.PixelHeight); 
     r.Measure(sz); 
     r.Arrange(new Rect(sz)); 
     var rtb = new RenderTargetBitmap(bitmap.PixelWidth, bitmap.PixelHeight, bitmap.DpiX, bitmap.DpiY, PixelFormats.Pbgra32); 
     rtb.Render(r); 

     PngBitmapEncoder png = new PngBitmapEncoder(); 
     png.Frames.Add(BitmapFrame.Create(rtb)); 

     Stream stm = File.Create("new.png"); 
     png.Save(stm); 
     stm.Close(); 
+0

comment allez-vous l'économie du contrôle? –

+0

ajouté le code à la question, merci! –

Répondre

0

Essayez ce morceau de code:

/// <summary> 
    /// Creates a screenshot of the given visual at the desired dots/inch (DPI). 
    /// </summary> 
    /// <param name="target">Visual component of which to capture as a screenshot.</param> 
    /// <param name="dpiX">Resolution, in dots per inch, in the X axis. Typical value is 96.0</param> 
    /// <param name="dpiY">Resolution, in dots per inch, in the Y axis. Typical value is 96.0</param> 
    /// <returns>A BitmapSource of the given Visual at the requested DPI, or null if there was an error.</returns>  
    public static BitmapSource CaptureScreen(Visual target, double dpiX, double dpiY) 
    { 
     if (target == null) 
     { 
      return null; 
     } 

     RenderTargetBitmap rtb = null; 

     try 
     { 
      // Get the actual size 
      Rect bounds = VisualTreeHelper.GetDescendantBounds(target); 
      rtb = new RenderTargetBitmap((int)(bounds.Width * dpiX/96.0), 
              (int)(bounds.Height * dpiY/96.0), 
              dpiX, 
              dpiY, 
              PixelFormats.Pbgra32); 

      DrawingVisual dv = new DrawingVisual(); 
      using (DrawingContext ctx = dv.RenderOpen()) 
      { 
       VisualBrush vb = new VisualBrush(target); 
       ctx.DrawRectangle(vb, null, new Rect(new Point(), bounds.Size)); 
      } 

      rtb.Render(dv); 
     } 
     catch (Exception ex) 
     { 
      Console.Error.WriteLine("Error capturing image: " + ex.Message); 
      return null; 
     } 

     return rtb; 
    } 
+0

n'a pas fonctionné ... l'image est tout rouge ... –

Questions connexes