0

I'm trying to render some text and an image to a writeable bitmap to make 1 larger image, and this method has worked in other locations for creating or manipulating images, but for some reason, this instance is only creating a black image. If I just set the image source to the original WriteableBitmap, it shows just fine, but when I call SaveJpeg and then LoadJpeg, it shows as a black image (and yes, I need to call SaveJpeg since this is actually getting passed up to a server). The following is how I'm trying to render the elements:

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)
Daniel
  • 73
  • 2
  • 10
  • How big (resolution wise) is your jpg? Can you change the quality from 100 to 95 and tell if it still happens (only asking because I never use 100, always use 95 and I have similar code in numerous places, working) – Shahar Prish Apr 09 '12 at 20:10
  • It's 929 x 929, and changing the quality doesn't seem to fix anything. – Daniel Apr 09 '12 at 20:18

1 Answers1

1

So apparently WriteableBitmap will render a transparent background as black when calling SaveJpeg, so I solved this by rendering a white canvas as well, like so:

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

messagePicture.Render(background, new TranslateTransform());
Daniel
  • 73
  • 2
  • 10