WriteableBitmap.SaveJpeg呈现黑色图像(WP7)

时间:2012-04-09 18:19:27

标签: c# windows-phone-7 writeablebitmap

我正在尝试将一些文本和图像渲染到可写位图以生成1个更大的图像,并且此方法已在其他位置用于创建或操作图像,但由于某种原因,此实例仅创建黑色图片。如果我只是将图像源设置为原始的WriteableBitmap,它显示就好了,但是当我调用SaveJpeg然后调用LoadJpeg时,它显示为黑色图像(是的,我需要调用SaveJpeg,因为这实际上已经传递给了服务器)。以下是我试图渲染元素的方法:

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)

1 个答案:

答案 0 :(得分:1)

显然,当调用SaveJpeg时,WriteableBitmap会将透明背景呈现为黑色,所以我通过渲染白色画布解决了这个问题,如下所示:

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

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