Search code examples
c#visual-studiopicturebox

move the origin of an image to bring it back to the center in c#


I am working on Windows Forms Applications, i have loaded an image into a pictureBox. I want to know if it's possible to move origin of image and bring it back to the center.

I have this : image1 I want this : image2 Thanks.


Solution

  • You could snap an area of the image and display this instead. This could look as follows:

    var nX = bmpOriginalImage.Width / 2;
    var nY = bmpOriginalImage.Height / 2;
    var nWidth = bmpOriginalImage.Width - nX;
    var nHeight = bmpOriginalImage.Height - nY;
    
    var rect = new Rectangle(nX, nY, nWidth, nHeight);
    
    var bmpSnapImage = bmpOriginalImage.Clone(rect, bmpOriginalImage.PixelFormat);
    
    m_pbxPictureBox.Image = bmpSnapImage;