C#合并4x图像创建单个图像,但使用这些特殊图像不起作用
我已经编码,这样4x图像将合并成单个图像,尺寸将相加,以便最终图像确保它始终有空间容纳所有4个图像C#合并4x图像创建单个图像,但使用这些特殊图像不起作用,c#,winforms,visual-studio-2015,C#,Winforms,Visual Studio 2015,我已经编码,这样4x图像将合并成单个图像,尺寸将相加,以便最终图像确保它始终有空间容纳所有4个图像 // Determining width and height int widthF = Math.Max((globeVar.width1 + globeVar.width2), (globeVar.width3 + globeVar.width4)); int heightF = Math.Max((globeVar.height1 + globeVar.height2), (globeVa
// Determining width and height
int widthF = Math.Max((globeVar.width1 + globeVar.width2), (globeVar.width3 + globeVar.width4));
int heightF = Math.Max((globeVar.height1 + globeVar.height2), (globeVar.height3 + globeVar.height4));
//Getting drawing objects ready
Image img = new Bitmap(widthF, heightF);
Graphics drawing = Graphics.FromImage(img);
//paint the background to check where image is not merged
drawing.Clear(Color.Blue);
drawing.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
// Draw the image to the graphics to create the new image
// Which will be used in the onpaint background
drawing.DrawImage(globeVar.image1, 0, 0);
drawing.DrawImage(globeVar.image2, (img.Width) / 2, 0);
drawing.DrawImage(globeVar.image3, 0, (img.Height) / 2);
drawing.DrawImage(globeVar.image4, (img.Width) / 2, (img.Height) / 2);
drawing.Save();
img.Save(@globeVar.savePath, ImageFormat.Png);
drawing.Dispose();
现在代码实际上适用于以下4个图像:
但当我使用这些图像时:
合并会变成这样:
我到底错过了什么
编辑:
我用于选择图像的代码:
//Getting The Image From The System
OpenFileDialog open = new OpenFileDialog();
open.Filter =
"Image Files(*.jpg; *.jpeg; *.gif; *.png; *.bmp)|*.jpg; *.jpeg; *.gif; *.png; *.bmp";
if (open.ShowDialog() == DialogResult.OK)
{
Bitmap img = new Bitmap(open.FileName);
globeVar.image4 = Image.FromFile(open.FileName);
globeVar.width4 = img.Width;
globeVar.height4 = img.Height;
}
这是我用于全局变量的代码,可以帮助我在项目中的任何地方使用它们:
public static class globeVar
{
public static int width1 = 0;
public static int width2 = 0;
public static int width3 = 0;
public static int width4 = 0;
public static int height1 = 0;
public static int height2 = 0;
public static int height3 = 0;
public static int height4 = 0;
public static Image image1;
public static Image image2;
public static Image image3;
public static Image image4;
public static string savePath = "C:\\Users\\Suleman\\Desktop\\ImageMerge\\Sample Images\\test.png";
//public static string savePath = "test.png";
}
我自己找到了答案。看起来DrawImage方法没有考虑图像的默认大小。因此,为了成功合并,我必须定义它们的原始大小 我刚刚更新了:
drawing.DrawImage(globeVar.image1, 0, 0, globeVar.width1, globeVar.height1);
drawing.DrawImage(globeVar.image2, (img.Width) / 2, 0, globeVar.width2, globeVar.height2);
drawing.DrawImage(globeVar.image3, 0, (img.Height) / 2, globeVar.width3, globeVar.height3);
drawing.DrawImage(globeVar.image4, (img.Width) / 2, (img.Height) / 2, globeVar.width4, globeVar.height4);
从文档:
我以前用过:DrawImage(图像, Int32, Int32)
我现在使用:DrawImage(图像, Int32, Int32, Int32, Int32)我们可以尝试调试它。尝试将图像拉伸到与正常工作的图像相同的分辨率,然后发布结果。图像的DPI是否相同?它们大小相同吗?这些所有的图像大小都相同。彩色图像较大,而卧室图像较小,但大小相同。