C# 如何在pictureBox中重复图片

C# 如何在pictureBox中重复图片,c#,.net,winforms,picturebox,C#,.net,Winforms,Picturebox,图像的宽度为200px,pictureBox的宽度为400px 我应该设置pictureBox的哪个属性来显示图像repeat-x?我认为没有任何属性可以使图像水平重复显示x次。但一点自定义代码可能会有所帮助,以下是我的代码: public static class PictureBoxExtension { public static void SetImage(this PictureBox pic, Image image, int repeatX) { B

图像的宽度为200px,pictureBox的宽度为400px


我应该设置pictureBox的哪个属性来显示图像repeat-x?

我认为没有任何属性可以使图像水平重复显示x次。但一点自定义代码可能会有所帮助,以下是我的代码:

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.
公共静态类PictureBoxExtension
{
公共静态void SetImage(此PictureBox pic,图像图像,int repeatX)
{
位图bm=新位图(image.Width*repeatX,image.Height);
Graphics gp=Graphics.FromImage(bm);

对于(int x=0;x默认的
BackgroundImageLayout=ImageLayout.Tile;
有什么问题?请确切说明您试图实现的目标。是否要收缩图像并在
PictureBox
中重复多次?@codecamp是的,我想在PictureBox中重复多次图像,但无需收缩或者缩放它。@Ice,那么当你使用
ImageLayout.Tile
时会发生什么呢?它会收缩和扭曲图像吗?也许@Ice不是在谈论ImagebackGround..但是图像属性..@matzone是对的。我没有说清楚。我是在询问图像属性!谢谢。