0

我正在尝试将一些文本和图像渲染到可写位图以制作 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)
4

1 回答 1

1

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

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

messagePicture.Render(background, new TranslateTransform());
于 2012-04-10T22:08:35.640 回答