2012-04-09 71 views
0

我想呈现一些文字和图像到一个可写入的位图,使一个更大的图像,并且此方法已在其他位置创建或操作图像,但为某些原因,这个实例只是创建一个黑色图像。如果我只是将图像源设置为原始的WriteableBitmap,它显示得很好,但是当我调用SaveJpeg然后调用LoadJpeg时,它显示为黑色图像(并且是的,我需要调用SaveJpeg,因为实际上它正在传递给服务器)。以下是如何我试图再现元素:WriteableBitmap.SaveJpeg呈现一个黑色图像(WP7)

NoteViewModel note = Instance.Note; 
var grid = new Grid() 
{ 
    Height = 929, 
    Width = 929 
}; 
grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(679) }); 
grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) }); 
var noteText = new TextBlock() 
{ 
    Text = note.Text, 
    FontFamily = note.FontFamily, 
    Foreground = note.FontColor, 
    TextWrapping = System.Windows.TextWrapping.Wrap, 
    Width = 929, 
    Height = 679 
}; 
Grid.SetRow(noteText, 0); 
grid.Children.Add(noteText); 

WriteableBitmap sigImage = Instance.Signature.SignatureImage; 
var sig = new Image() 
{ 
    Source = sigImage, 
    Height = 250, 
    Width = (sigImage.PixelWidth/sigImage.PixelHeight) * 250, 
    Margin = new Thickness(929 - ((sigImage.PixelWidth/sigImage.PixelHeight) * 250), 0, 0, 0) 
}; 
Grid.SetRow(sig, 1); 
grid.Children.Add(sig); 

var messagePicture = new WriteableBitmap(grid, null); 

var stream = new MemoryStream(); 

messagePicture.SaveJpeg(stream, messagePicture.PixelWidth, messagePicture.PixelHeight, 0, 100); //Save to a temp stream 

stream.Position = 0; 

var test = new WriteableBitmap(929,929); //Load the picture back up to see it 
test.LoadJpeg(stream); 

img.Source = test; //Show the image on screen (img is an Image element) 
+0

你的jpg有多大(分辨率明智)?你可以改变质量从100到95,并告诉它是否仍然发生(只问,因为我从来没有使用100,总是使用95,我有许多地方类似的代码,工作) – 2012-04-09 20:10:23

+0

它是929 x 929,改变质量doesn' t似乎可以解决任何问题。 – Daniel 2012-04-09 20:18:04

回答

1

因此很明显,WriteableBitmap的将调用SaveJpeg时呈现透明背景为黑色,所以我解决了这个通过渲染白色帆布为好,像这样:

var background = new Canvas() 
{ 
    Width = 929, 
    Height = 929, 
    Background = new SolidColorBrush(Colors.White) 
}; 

messagePicture.Render(background, new TranslateTransform()); 
+0

任何想法如何我可以在WPF中实现这一点。 – 2014-02-13 19:35:58