正在使用C#

时遇到类似下面的位图图像

“替代文字”

我想建立一个重复的图象象下面在水平位置得到repeted连续图像对于一些给定的宽度。我的意思我喜欢画画重复图像像下面从上面的单个位(简单地说,在HTML中我们可以有一个形象,并设置重复X得到重复image.like说)我怎么能在C#中做到这一点。

“替代文字”

这样我可以在我的应用程序绘制新位图。这该怎么做。?

有帮助吗?

解决方案

您可以做这样的:

Bitmap myImage = new Bitmap(50, 50); //assuming you want you image to be 50,50
Bitmap originalImage = new Bitmap("myPngSource.png"); //original image to copy

using (Graphics g = Graphics.FromImage(myImage))
{
     g.DrawImage(originalImage, new Rectangle(0, 0, originalImage.Width, originalImage.Height));
}

MemoryStream ms = new MemoryStream();
myImage.Save(ms, ImageFormat.Png);

BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = ms;
bi.EndInit();

MyImageControl.Source = bi;

或者类似的东西,这是未经测试,我只是撕开出来一个小工具程序,我前一段时间做的。我希望它可以帮助...你只需要改变最终图像的宽度和做一个循环在g.DrawImage调用由originalImage的宽度增加第二个参数。 (即,如果你想5次重复,for循环做5次)

HTH --mark

其他提示

您不需要创建其他位图。这是绘制位图的问题。在地方,你darw位图使用 的drawImage方法几次,并通过它的宽度递增位图的X位置。比方说16是图像的宽度。确保位图已被初始化。

private void Form1_Paint(object sender, PaintEventArgs e)
{

    e.Graphics.DrawImage(bmp,x,y);
    e.Graphics.DrawImage(bmp,x+16,y);
    e.Graphics.DrawImage(bmp,x+32,y);
}
//x- integer value represents no. of times images to repeated horizontally
var destImage = new Bitmap(sourceImage.Width * x, sourceImage.Height, PixelFormat.Format32bppArgb);
using (TextureBrush brush = new TextureBrush(sourceImage, WrapMode.Tile))
using (Graphics g = Graphics.FromImage(destImage))
{
// Do your drawing here
g.FillRectangle(brush, 0, 0, destImage.Width, destImage.Height);
destImage.Save(@"C:\sourceImage.png", ImageFormat.Png); 
//mention path of image to save, if needed
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top