在C#.Net中合并两个图像以创建单个图像
我有一个要求,我需要使用C#.Net将两个不同的png/jpeg图像合并成一个图像。将有一个特定的位置上定义的源图像,我需要插入另一个图像。有人能推荐一些链接吗?免责声明:我在Atalasoft工作 我们(这是免费的)可以做到这一点 打开图像在C#.Net中合并两个图像以创建单个图像,c#,.net,image-manipulation,C#,.net,Image Manipulation,我有一个要求,我需要使用C#.Net将两个不同的png/jpeg图像合并成一个图像。将有一个特定的位置上定义的源图像,我需要插入另一个图像。有人能推荐一些链接吗?免责声明:我在Atalasoft工作 我们(这是免费的)可以做到这一点 打开图像 AtalaImage botImage = new AtalaImage("bottomImage.png"); AtalaImage topImage = new AtalaImage("topImage.png"); 把一个叠在另一个上面 Po
AtalaImage botImage = new AtalaImage("bottomImage.png");
AtalaImage topImage = new AtalaImage("topImage.png");
把一个叠在另一个上面
Point pos = new Point(0,0); // or whatever you need
OverlayCommand cmd = new OverlayCommand(topImage, pos);
ImageResults res = cmd.Apply(botImage);
如果需要生成不同大小的图像,请查看CanvasCommand
。您还可以创建一个所需大小的图像,然后将每个图像覆盖到该图像上
挽救
botImage.Save("newImage.png", new PngEncoder(), null);
此方法将两个图像一个合并到另一个图像的顶部,您可以修改代码以满足您的需要:
public static Bitmap MergeTwoImages(Image firstImage, Image secondImage)
{
if (firstImage == null)
{
throw new ArgumentNullException("firstImage");
}
if (secondImage == null)
{
throw new ArgumentNullException("secondImage");
}
int outputImageWidth = firstImage.Width > secondImage.Width ? firstImage.Width : secondImage.Width;
int outputImageHeight = firstImage.Height + secondImage.Height + 1;
Bitmap outputImage = new Bitmap(outputImageWidth, outputImageHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
using (Graphics graphics = Graphics.FromImage(outputImage))
{
graphics.DrawImage(firstImage, new Rectangle(new Point(), firstImage.Size),
new Rectangle(new Point(), firstImage.Size), GraphicsUnit.Pixel);
graphics.DrawImage(secondImage, new Rectangle(new Point(0, firstImage.Height + 1), secondImage.Size),
new Rectangle(new Point(), secondImage.Size), GraphicsUnit.Pixel);
}
return outputImage;
}
在这一切之后,我找到了一个新的更简单的方法尝试这个 它可以将多张照片连接在一起:
public static System.Drawing.Bitmap CombineBitmap(string[] files)
{
//read all images into memory
List<System.Drawing.Bitmap> images = new List<System.Drawing.Bitmap>();
System.Drawing.Bitmap finalImage = null;
try
{
int width = 0;
int height = 0;
foreach (string image in files)
{
//create a Bitmap from the file and add it to the list
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(image);
//update the size of the final bitmap
width += bitmap.Width;
height = bitmap.Height > height ? bitmap.Height : height;
images.Add(bitmap);
}
//create a bitmap to hold the combined image
finalImage = new System.Drawing.Bitmap(width, height);
//get a graphics object from the image so we can draw on it
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(finalImage))
{
//set background color
g.Clear(System.Drawing.Color.Black);
//go through each image and draw it on the final image
int offset = 0;
foreach (System.Drawing.Bitmap image in images)
{
g.DrawImage(image,
new System.Drawing.Rectangle(offset, 0, image.Width, image.Height));
offset += image.Width;
}
}
return finalImage;
}
catch (Exception)
{
if (finalImage != null)
finalImage.Dispose();
//throw ex;
throw;
}
finally
{
//clean up memory
foreach (System.Drawing.Bitmap image in images)
{
image.Dispose();
}
}
}
public static System.Drawing.Bitmap组合位图映射(字符串[]文件)
{
//将所有图像读入内存
列表图像=新列表();
System.Drawing.Bitmap finalImage=null;
尝试
{
整数宽度=0;
整数高度=0;
foreach(文件中的字符串图像)
{
//从文件创建位图并将其添加到列表中
System.Drawing.Bitmap Bitmap=新的System.Drawing.Bitmap(图像);
//更新最终位图的大小
宽度+=位图宽度;
高度=位图。高度>高度?位图。高度:高度;
添加(位图);
}
//创建位图以保存组合图像
finalImage=新系统.绘图.位图(宽度,高度);
//从图像中获取图形对象,以便我们可以在其上绘制
使用(System.Drawing.Graphics g=System.Drawing.Graphics.FromImage(最终图像))
{
//设置背景色
g、 清晰(系统、图纸、颜色、黑色);
//浏览每个图像并在最终图像上绘制
整数偏移=0;
foreach(图像中的System.Drawing.Bitmap图像)
{
g、 DrawImage(图像,
新系统。绘图。矩形(偏移量,0,图像。宽度,图像。高度);
偏移量+=图像宽度;
}
}
返回最终授权;
}
捕获(例外)
{
if(finalImage!=null)
finalImage.Dispose();
//掷骰子;
投掷;
}
最后
{
//清理内存
foreach(图像中的System.Drawing.Bitmap图像)
{
image.Dispose();
}
}
}
我有一个api。此api将组合2张图片。1张图片是您的图片/设计/海报,其他图片是广告牌的图片。通过使用API,您可以看到您的设计/图像/海报在billboard照片中的样子DotImage照片SDK不再免费:(很抱歉,我不再在那里工作了。这个问题的其他答案应该仍然适用于你。我也在寻找同样的答案,发现了这个。太棒了,谢谢你的代码-但从来没有“扔ex”-总是“扔”。您正在重置堆栈,使其无法进行其他调试!此代码生成的最终图像的宽度是原始图像的两倍(高度保持正常)@PabloCosta是的,如果您看到上面的代码height=bitmap.height>height?bitmap.height:height;
新创建的图像的高度将是所有图像中的最大值。为什么格式化32bppargb?@Toolkit:据我所知,当您使用仅包含两个参数的构造函数重载时(宽度,高度)
它将创建一个24bit
版本,即Format24bppRgb
。因此,如果包含一个原始图像,则会丢失原始图像的alpha通道信息,因此会生成Format32bppArgb
。不过,我相信如果您从原始图像而不是内联表单获得适当的格式,会更方便at32bppArgb
东西…效果不错,但我认为两幅图像之间不需要增加1个像素的高度。我尝试了类似的方法。我设置了更大的宽度,当我绘制图像时,会显示一个灰色区域。我如何将该区域更改为白色或我喜欢的其他纯色?
private void Merge _Click(object sender, EventArgs e)
{
}
DirectoryInfo directory=new DirectoryInfo("D:\\Images");
if(directory!=null)
{
FileInfo[]files = directory.GetFiles();
MergeImages(Image);
}
private void MergeImages(FileInfo[] Image)
{
//change the location to store the final image.
string FImage= @"D:\\Images\\FImage.jpg";
List imageHeights = new List();
int nIndex = 0;
int width = 0;
foreach (FileInfo file in files)
{
Image img = Image.FromFile(file.FullName);
imageHeights.Add(img.Height);
width += img.Width;
img.Dispose();
}
imageHeights.Sort();
int height = imageHeights[imageHeights.Count - 1];
Bitmap NewImg = new Bitmap(width, height);
Graphics Gr= Graphics.FromImage(NewImg);
Gr.Clear(SystemColors.AppWorkspace);
foreach (FileInfo file in files)
{
Image img = Image.FromFile(file.FullName);
if (nIndex == 0)
{
Gr.DrawImage(img, new Point(0, 0));
nIndex++;
width = img.Width;
}
else
{
Gr.DrawImage(img, new Point(width, 0));
width += img.Width;
}
img.Dispose();
}
Gr.Dispose();
NewImg .Save(FImage, System.Drawing.Imaging.ImageFormat.Jpeg);
NewImg .Dispose();
imageLocation.Image = Image.FromFile(FImage);
}
private void Merge _Click(object sender, EventArgs e)
{
}
DirectoryInfo directory=new DirectoryInfo("D:\\Images");
if(directory!=null)
{
FileInfo[]files = directory.GetFiles();
MergeImages(Image);
}
private void MergeImages(FileInfo[] Image)
{
//change the location to store the final image.
string FImage= @"D:\\Images\\FImage.jpg";
List imageHeights = new List();
int nIndex = 0;
int width = 0;
foreach (FileInfo file in files)
{
Image img = Image.FromFile(file.FullName);
imageHeights.Add(img.Height);
width += img.Width;
img.Dispose();
}
imageHeights.Sort();
int height = imageHeights[imageHeights.Count - 1];
Bitmap NewImg = new Bitmap(width, height);
Graphics Gr= Graphics.FromImage(NewImg);
Gr.Clear(SystemColors.AppWorkspace);
foreach (FileInfo file in files)
{
Image img = Image.FromFile(file.FullName);
if (nIndex == 0)
{
Gr.DrawImage(img, new Point(0, 0));
nIndex++;
width = img.Width;
}
else
{
Gr.DrawImage(img, new Point(width, 0));
width += img.Width;
}
img.Dispose();
}
Gr.Dispose();
NewImg .Save(FImage, System.Drawing.Imaging.ImageFormat.Jpeg);
NewImg .Dispose();
imageLocation.Image = Image.FromFile(FImage);
}