在水平位置重复图像(C#图)

时间:2010-08-10 07:20:25

标签: c# wpf system.drawing bitmapimage

我正在使用c#

我的位图图像如下所示

alt text http://img210.imageshack.us/img210/2460/89372850.jpg

我希望在水平位置创建如下所示的重复图像,以获得某些给定宽度的重复连续图像。我的意思是我喜欢从上面的单个位图中绘制如下所示的重复图像(简单来说,在html中我们可以有一个图像并设置重复X以获得重复的图像。就像那样)我如何在c#中执行此操作。

alt text http://img59.imageshack.us/img59/7663/26679829.jpg

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

3 个答案:

答案 0 :(得分:0)

您不需要创建其他位图。这是绘制位图的问题。在你使用位图的地方 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);
}

答案 1 :(得分:0)

你可以这样做:

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调用上循环,将第二个参数增加原始图像的宽度。 (即如果你想重复5次,那就做5次for循环)

HTH --mark

答案 2 :(得分:0)

//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
}