c#:生成一个新的单个图像,在水平方向上重复另一个图像x次
我正在寻找执行以下操作的.NET代码示例(System.Drawing.Image): 加载给定的图像文件。c#:生成一个新的单个图像,在水平方向上重复另一个图像x次,c#,.net,image,C#,.net,Image,我正在寻找执行以下操作的.NET代码示例(System.Drawing.Image): 加载给定的图像文件。 生成一个新的单个图像,将原始图像水平重复x次。这将创建一个新位图,并将源位图绘制到它的numTimes次 Bitmap b = Bitmap.FromFile(sourceFilename); Bitmap output = new Bitmap(b.Width * numTimes, b.Height); Graphics g = Graphics.FromImage(output);
生成一个新的单个图像,将原始图像水平重复x次。这将创建一个新位图,并将源位图绘制到它的
numTimes
次
Bitmap b = Bitmap.FromFile(sourceFilename);
Bitmap output = new Bitmap(b.Width * numTimes, b.Height);
Graphics g = Graphics.FromImage(output);
for (int i = 0; i < numTimes; i++) {
g.DrawImage(b, i * b.Width, 0);
}
// do whatever with the image, here we'll output it
output.Save(outputFilename);
// make sure to clean up too
g.Dispose();
b.Dispose();
output.Dispose();
Bitmap b=Bitmap.FromFile(sourceFilename);
位图输出=新位图(b.宽度*numTimes,b.高度);
Graphics g=Graphics.FromImage(输出);
对于(int i=0;i
这里是复制目标位图上每个源图像像素的示例
static void Main(string[] args)
{
ushort nbCopies = 2;
Bitmap srcBitmap = (Bitmap)Image.FromFile(@"C:\Users\Public\Pictures\Sample Pictures\Koala.jpg");
Bitmap dstBitmap = new Bitmap(srcBitmap.Width * nbCopies, srcBitmap.Height, srcBitmap.PixelFormat);
//Slow method
for (int curCopy = 0; curCopy < nbCopies; curCopy++)
{
for (int x = 0; x < srcBitmap.Width; x++)
{
for (int y = 0; y < srcBitmap.Height; y++)
{
Color c = srcBitmap.GetPixel(x, y);
dstBitmap.SetPixel(x + (curCopy * srcBitmap.Width), y, c);
}
}
}
//OR
//Fast method using unsafe code
BitmapData srcBd = srcBitmap.LockBits(new Rectangle(Point.Empty, srcBitmap.Size), ImageLockMode.ReadOnly, srcBitmap.PixelFormat);
BitmapData dstBd = dstBitmap.LockBits(new Rectangle(Point.Empty, dstBitmap.Size), ImageLockMode.WriteOnly, dstBitmap.PixelFormat);
unsafe
{
for (int curCopy = 0; curCopy < nbCopies; curCopy++)
{
for (int y = 0; y < srcBitmap.Height; y++)
{
byte* srcRow = (byte*)srcBd.Scan0 + (y * srcBd.Stride);
byte* dstRow = (byte*)dstBd.Scan0 + (y * dstBd.Stride) + (curCopy * srcBd.Stride);
for (int x = 0; x < srcBitmap.Width; x++)
{
//Copy each composant value (typically RGB)
for (int comp = 0; comp < (srcBd.Stride / srcBd.Width); comp++)
{
dstRow[x * 3 + comp] = srcRow[x * 3 + comp];
}
}
}
}
}
dstBitmap.UnlockBits(dstBd);
srcBitmap.UnlockBits(srcBd);
dstBitmap.Save(@"C:\Users\Public\Pictures\Sample Pictures\Koala_multiple.jpg");
dstBitmap.Dispose();
srcBitmap.Dispose();
}
static void Main(字符串[]args)
{
ushort nbCopies=2;
位图srcbimat=(位图)Image.FromFile(@“C:\Users\Public\Pictures\Sample Pictures\Koala.jpg”);
位图DSTBAMIT=新位图(srcBitmap.Width*nbCopies,srcBitmap.Height,srcBitmap.PixelFormat);
//慢法
对于(int curCopy=0;curCopy
不要显式调用g和b上的Dispose,而应该将其包装在using块中。