locked
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

Answers

  • 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.PixelWidth,  
      source.PixelHeight,  
      source.DpiX, source.DpiY,  
      source.Format, null); 
     
    
    target.WritePixels( 
      new Int32Rect(X, Y, source.PixelWidth, source.PixelHeight),  
      data, stride, 0); 
     
    targetImage.Source = target; 
    
    

     

    Regards,
    Pedro


    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