How to paste image to white bitmap canvas? RRS feed

  • Question

  • Hi. Lets say I have image 100x50. I want to paste this image to 200x200 white background bitmap so that the image is in the center/middle. How can I create such a .jpg image?
    Monday, February 6, 2012 4:23 PM


  • Hi there


    I think the code below should do what you're trying to do. You simply need to define X & Y as the offset position:


    int stride = source.PixelWidth * (source.Format.BitsPerPixel / 8); 
    byte[] data = new byte[stride * source.PixelHeight]; 
    source.CopyPixels(data, stride, 0); 
    WriteableBitmap target = new WriteableBitmap( 
      source.DpiX, source.DpiY,  
      source.Format, null); 
      new Int32Rect(X, Y, source.PixelWidth, source.PixelHeight),  
      data, stride, 0); 
    targetImage.Source = target; 



    If you find my post helpful, please remember to "Mark As Answer" and/or "Vote as Helpful"
    • Marked as answer by GaiusBaltar Tuesday, February 7, 2012 7:37 AM
    Monday, February 6, 2012 4:33 PM