Search code examples
c#windows-phone-8

How can I write a image onto another?


I have a image that is 500x500, and to this image i want to copy in a symbol that is 32x32, in the upper right corner of the 500x500 image.

I've tried to Render it in, but it does not seem to work:

var statusSymbol = new Image()
{
      Source = new BitmapImage(new Uri(tempJPEG, UriKind.Relative))
};

WriteableBitmap wb = new WriteableBitmap(bitmap);
wb.Render(statusSymbol , new TranslateTransform() { X = 500-10, Y = 10 });
wb.Invalidate();

Solution

  • The manual way (with its pros and cons):

     public Bitmap AddLogo(Bitmap original_image, Bitmap logo)
        {
            /// Add validation here (not null, logo smaller then original, etc..)
            Bitmap with_logo = new Bitmap(original_image);           
    
            /// To get the right corner use:
            int x_start_value = (original_image.Width - logo.Width) - 1;
            int x_stop_value = original_image.Width -1;
            /// 
    
            /// You can add ofset (padding) by starting x and\or y from your value insted of 0
    
            for (int y = 0; y < logo.Height; y++)            
            {
    
                /// For left corner
                ///for (int x = 0; x < logo.Width; x++)
                int logo_x = 0;
                for (int x = x_start_value; x < x_stop_value; x++)                
                {
                    with_logo.SetPixel(x, y, logo.GetPixel(logo_x, y));
                    logo_x++;
                }
            }
    
            return with_logo;
        }