简体   繁体   中英

How can I repeat a picture in pictureBox

The image has 200px width and the pictureBox has 400px.

Which property of pictureBox should I set to display the image repeat-x?

I don't think there is any property which makes the image display x times repeatedly horizontally. But a little custom code can help, here is my code:

public static class PictureBoxExtension
{
    public static void SetImage(this PictureBox pic, Image image, int repeatX)
    {
        Bitmap bm = new Bitmap(image.Width * repeatX, image.Height);
        Graphics gp = Graphics.FromImage(bm);
        for (int x = 0; x <= bm.Width - image.Width; x += image.Width)
        {
            gp.DrawImage(image, new Point(x, 0));
        }
        pic.Image = bm;
    }
}

//Now you can use the extension method SetImage to make the PictureBox display the image x-repeatedly 
myPictureBox.SetImage(myImage, 3);//repeat 3 images horizontally.

You can customize the code to make it repeat vertically or look like a check board.

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM