Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 合并多个图像后,最终图像具有更小的图像_C# - Fatal编程技术网

C# 合并多个图像后,最终图像具有更小的图像

C# 合并多个图像后,最终图像具有更小的图像,c#,C#,我试图创建一个大的最终jpg图像从4个不同的jpg图像。我注意到在最终图像上添加图像后,图像的大小发生了变化 最终图像上的图像大小也应保持不变 Ex-与实际图像相比,最终图像上的星系图像(暗天空)看起来很小 任何帮助都将不胜感激 private void CombineImages() { string path1 = @"C:\temp\"; DirectoryInfo directory = new DirectoryInfo(path1)

我试图创建一个大的最终jpg图像从4个不同的jpg图像。我注意到在最终图像上添加图像后,图像的大小发生了变化

最终图像上的图像大小也应保持不变

Ex-与实际图像相比,最终图像上的星系图像(暗天空)看起来很小

任何帮助都将不胜感激

     private void CombineImages()
    {
        string path1 = @"C:\temp\";
        DirectoryInfo directory = new DirectoryInfo(path1);

        //change the location to store the final image.
        FileInfo[] files = directory.GetFiles();
        string finalImage = @"C:\\images\\FinalImage3.jpg";
        List<int> imageHeights = new List<int>();
        List<int> imagewidths = new List<int>();
        int nIndex = 0;
        int width = 0;
        int maxHeight = 0;
        int totalHeight = 0;
        bool odd = true;
        bool firstRow = true;

        //to get height and width to create final image
        foreach (FileInfo file in files)
        {

            if (odd)
            {
                if (firstRow)
                {
                    Image img = Image.FromFile(file.FullName);
                    firstRow = false;
                    imageHeights.Add(img.Height);
                    width += img.Width;
                    img.Dispose();
                    odd = false;
                }
                else
                {
                    Image img = Image.FromFile(file.FullName);
                    maxHeight = imageHeights.Max();
                    imagewidths.Add(width+100);
                    width = 0;
                    totalHeight = totalHeight + maxHeight+img.Height+100;
                    imageHeights.Clear();
                    imageHeights.Add(img.Height);
                    width += img.Width;
                    img.Dispose();
                    odd = false;
                }


            }
            else
            {
                Image img = Image.FromFile(file.FullName);
                imageHeights.Add(img.Height);
                width += img.Width;
                img.Dispose();
                odd = true;
            }


        }
        imageHeights.Sort();

        Bitmap img3 = new Bitmap(imagewidths.Max(), totalHeight);

        Graphics g = Graphics.FromImage(img3);
        g.Clear(Color.Gainsboro);


         imageHeights = new List<int>();
         imagewidths = new List<int>();
         nIndex = 0;
         width = 0;
         maxHeight = 0;
         totalHeight = 0;
         odd = true;
         firstRow = true;

        int imagewidth = 0;

        //actual merging of images
        foreach (FileInfo file in files)
        {
            Image img = Image.FromFile(file.FullName);

            if (odd)
            {
                if (firstRow)
                {
                    g.DrawImage(img, 25, 25);
                    firstRow = false;
                    imageHeights.Add(img.Height);
                    width += img.Width;
                    img.Dispose();
                    odd = false;
                }
                else
                {
                    maxHeight = imageHeights.Max();
                    g.DrawImage(img, 25, maxHeight+50);
                    imagewidths.Add(width);
                    width = 0;
                    totalHeight = totalHeight + maxHeight + img.Height;
                    imageHeights.Clear();
                    imageHeights.Add(img.Height);
                    width += img.Width;
                    imagewidth = img.Width;
                    img.Dispose();
                    odd = false;
                }


            }
            else
            {

                imageHeights.Add(img.Height);
                g.DrawImage(img, width+50, maxHeight+50);

                img.Dispose();

                odd = true;
            }

            img.Dispose();
        }
        g.Dispose();
        System.Drawing.Imaging.Encoder myEncoder =
            System.Drawing.Imaging.Encoder.Quality;
        EncoderParameters myEncoderParameters = new EncoderParameters(1);
        EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 50L);
        myEncoderParameters.Param[0] = myEncoderParameter;
        ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);
        img3.Save(finalImage, jpgEncoder, myEncoderParameters);
        img3.Dispose();

    }

    private ImageCodecInfo GetEncoder(ImageFormat format)
    {
        ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
        foreach (ImageCodecInfo codec in codecs)
        {
            if (codec.FormatID == format.Guid)
            {
                return codec;
            }
        }
        return null;
    }
private void CombineImages()
{
字符串路径1=@“C:\temp\”;
DirectoryInfo目录=新的DirectoryInfo(路径1);
//更改位置以存储最终图像。
FileInfo[]files=目录.GetFiles();
字符串finalImage=@“C:\\images\\FinalImage3.jpg”;
List imageHeights=新列表();
List imagewidths=新列表();
int-nIndex=0;
整数宽度=0;
int maxHeight=0;
int totalHeight=0;
布尔奇数=真;
bool firstRow=true;
//获取高度和宽度以创建最终图像
foreach(文件中的文件信息文件)
{
if(奇数)
{
如果(第一行)
{
Image img=Image.FromFile(file.FullName);
第一行=假;
图像高度。添加(图像高度);
宽度+=最小宽度;
img.Dispose();
奇数=假;
}
其他的
{
Image img=Image.FromFile(file.FullName);
maxHeight=imageHeights.Max();
图像宽度。添加(宽度+100);
宽度=0;
总高度=总高度+最大高度+惯性高度+100;
imageHeights.Clear();
图像高度。添加(图像高度);
宽度+=最小宽度;
img.Dispose();
奇数=假;
}
}
其他的
{
Image img=Image.FromFile(file.FullName);
图像高度。添加(图像高度);
宽度+=最小宽度;
img.Dispose();
奇数=真;
}
}
imageHeights.Sort();
位图img3=新位图(imagewidths.Max(),totalHeight);
Graphics g=Graphics.FromImage(img3);
g、 清晰(颜色:Gainsboro);
imageHeights=新列表();
imagewidths=新列表();
nIndex=0;
宽度=0;
最大高度=0;
总高度=0;
奇数=真;
第一行=真;
int imagewidth=0;
//图像的实际合并
foreach(文件中的文件信息文件)
{
Image img=Image.FromFile(file.FullName);
if(奇数)
{
如果(第一行)
{
g、 绘图图像(img,25,25);
第一行=假;
图像高度。添加(图像高度);
宽度+=最小宽度;
img.Dispose();
奇数=假;
}
其他的
{
maxHeight=imageHeights.Max();
g、 图纸图像(img,25,最大高度+50);
图像宽度。添加(宽度);
宽度=0;
总高度=总高度+最大高度+最小高度;
imageHeights.Clear();
图像高度。添加(图像高度);
宽度+=最小宽度;
图像宽度=图像宽度;
img.Dispose();
奇数=假;
}
}
其他的
{
图像高度。添加(图像高度);
g、 图纸图像(img,宽度+50,最大高度+50);
img.Dispose();
奇数=真;
}
img.Dispose();
}
g、 处置();
系统.绘图.成像.编码器myEncoder=
系统、绘图、成像、编码器、质量;
EncoderParameters myEncoderParameters=新的EncoderParameters(1);
编码器参数myEncoderParameter=新编码器参数(myEncoder,50L);
myEncoderParameters.Param[0]=myEncoderParameter;
ImageCodeInfo jpgEncoder=GetEncoder(ImageFormat.Jpeg);
img3.Save(finalImage、jpgEncoder、myEncoderParameters);
img3.Dispose();
}
专用ImageCodeInfo GetEncoder(ImageFormat格式)
{
ImageCodecInfo[]codecs=ImageCodecInfo.GetImageDecoders();
foreach(编解码器中的ImageCodeInfo编解码器)
{
if(codec.FormatID==format.Guid)
{
返回编解码器;
}
}
返回null;
}

您确定这不仅仅是屏幕上图像的缩放吗?虽然阅读代码有点困难,但我看不到任何地方会绘制与原始图像大小不同的图像。我唯一能建议的是检查图像分辨率是否正确(请参阅中“备注”下关于此主题的备注)。您确定这不仅仅是屏幕上图像的缩放吗?虽然阅读代码有点困难,但我看不到任何地方会绘制与原始图像大小不同的图像。我唯一能建议的是检查图像分辨率是否正确(请参阅中“备注”下关于此主题的备注)。