1
votes

I am trying to render a string over an image chosen by user via Photochooser task. I have seen various replies to similar question but none of the replies have nailed it.

This is what I have come up with -

void photochoosertask_Completed(object sender, PhotoResult e)
        {
            if (e.TaskResult == TaskResult.OK)
            {
System.Windows.Media.Imaging.BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage();
                bmp.SetSource(e.ChosenPhoto);
                image1.Source = bmp;



                string steamer = "SO!";
                System.Windows.Media.Imaging.WriteableBitmap bmps = new System.Windows.Media.Imaging.WriteableBitmap(bmp);
                RenderString(bmps, steamer);




            }



        }
        private void RenderString(System.Windows.Media.Imaging.WriteableBitmap bitmap, string steamer)
        {


            textBlock1.Text = steamer;



            bitmap.Render(textBlock1 , null);

            bitmap.Invalidate();

        }
    }

The code however doesn't work. I am most likely doing a major mistake. Any help appreciated, thanks!

2
"doesn't work" is quite vague. Your chances of having your question answered would be greatly increased by describing what actually happens when you run this code. - MusiGenesis
doesn't work = the text doesn't get rendered ON the image. That's it. - Rahul Mathur

2 Answers

0
votes

According to the documentation:

If an empty transform is supplied [i.e. the null you're passing as the second parameter], the bits representing the element show up at the same offset as if they were placed within their parent.

So if I understand what's happening correctly (and I probably don't), your textBlock1 element is being rendered with the same offset as it has on your parent form. So it may be that textBlock1 is so far down from the top and left that it doesn't show up in your writeable bitmap.

BTW, I'm not familiar with WriteableBitmap, but what you're doing (putting text into a UI element and then rendering that element onto your bitmap) seems like a strange way to add text to a bitmap.

0
votes

I just figured it out. Thought I should post the solution code here, might help somebody - someday :)

//setup a writeable bitmap with required dimensions
System.Windows.Media.Imaging.WriteableBitmap wbmps = new System.Windows.Media.Imaging.WriteableBitmap(x,y);
//set up a transform, we'll use ScaleTransform and we'll keep things simple here, 1x on both the axis 
 ScaleTransform transform = new System.Windows.Media.ScaleTransform();
 transform.ScaleX=1;
 transform.ScaleY=1; 
//now we need to render the image on the writeablebitmap and follow it up by rendering a //string
wbmps.Render(imageelement,transform);
//Now render the string which is equivalent to TextBlock.Text
wbmps.Render(texblock,transform);
//Finally - redraw the writeablebitmap to complete the rendering
wbmps.Invalidate();