2012-05-03 1 views
1

J'essaie de générer une image tiff simple au moment de l'exécution. Cette image est constituée d'un arrière-plan blanc et d'une image téléchargée à partir du serveur distant.Création d'une image tiff à l'exécution dans WPF

Voici le code que j'ai écrit pour atteindre cet objectif:

 const string url = "http://localhost/barcode.gif"; 

     var size = new Size(794, 1123); 
     var drawingVisual = new DrawingVisual(); 

     using (var drawingContext = drawingVisual.RenderOpen()) 
     { 
      drawingContext.DrawRectangle(new SolidColorBrush(Colors.White), null, new Rect(size)); 

      var image = new BitmapImage(new Uri(url)); 
      drawingContext.DrawImage(image, new Rect(0, 0, 180, 120)); 
     } 

     var targetBitmap = new RenderTargetBitmap((int)size.Width, (int)size.Height, 96, 96, PixelFormats.Default); 
     targetBitmap.Render(drawingVisual); 

     var convertedBitmap = new FormatConvertedBitmap(targetBitmap, PixelFormats.BlackWhite, null, 0); 

     var encoder = new TiffBitmapEncoder(); 
     encoder.Frames.Add(BitmapFrame.Create(convertedBitmap)); 

     using (var fs = new FileStream("out.tif", FileMode.Create)) 
     { 
      encoder.Save(fs); 
     } 

Le code fonctionne et génère "out.tif" fichier. Mais le fichier de sortie a juste un fond blanc, sans image reçue du serveur distant.

Quel peut être le problème? J'ai essayé le code suivant de différentes façons, mais à chaque fois sans aucune chance.

Répondre

0

J'ai trouvé cette lecture sur la classe FormatConvertedBitmap. Peut-être faire un essai

 FormatConvertedBitmap newFormatedBitmapSource = new FormatConvertedBitmap(); 

     // BitmapSource objects like FormatConvertedBitmap can only have their properties 
     // changed within a BeginInit/EndInit block. 
     newFormatedBitmapSource.BeginInit(); 

     // Use the BitmapSource object defined above as the source for this new 
     // BitmapSource (chain the BitmapSource objects together). 
     newFormatedBitmapSource.Source = targetBitmap; 

     // Set the new format to BlackWhite. 
     newFormatedBitmapSource.DestinationFormat = PixelFormats.BlackWhite; 
     newFormatedBitmapSource.EndInit();