Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.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#/.NET中合并两个图像 简单的想法:我有两个我想合并的图像,一个是500 X500,中间是透明的,另一个是150×150。 基本思想是:创建一个空的画布,它是500×500,在空画布的中间放置150×150的图像,然后复制500×500的图像,使透明的中间部分允许150×150通过。_C#_.net_Image - Fatal编程技术网

在C#/.NET中合并两个图像 简单的想法:我有两个我想合并的图像,一个是500 X500,中间是透明的,另一个是150×150。 基本思想是:创建一个空的画布,它是500×500,在空画布的中间放置150×150的图像,然后复制500×500的图像,使透明的中间部分允许150×150通过。

在C#/.NET中合并两个图像 简单的想法:我有两个我想合并的图像,一个是500 X500,中间是透明的,另一个是150×150。 基本思想是:创建一个空的画布,它是500×500,在空画布的中间放置150×150的图像,然后复制500×500的图像,使透明的中间部分允许150×150通过。,c#,.net,image,C#,.net,Image,我知道如何用Java、PHP和Python来做。。。我只是不知道在C#中应该使用什么对象/类,一个简单的将一个图像复制到另一个图像的例子就足够了。这会将一个图像添加到另一个图像中 using (Graphics grfx = Graphics.FromImage(image)) { grfx.DrawImage(newImage, x, y) } 图形在名称空间系统中。绘图基本上我在我们的一个应用程序中使用它: 我们希望在视频帧上覆盖播放图标: Image playbutton; tr

我知道如何用Java、PHP和Python来做。。。我只是不知道在C#中应该使用什么对象/类,一个简单的将一个图像复制到另一个图像的例子就足够了。

这会将一个图像添加到另一个图像中

using (Graphics grfx = Graphics.FromImage(image))
{
    grfx.DrawImage(newImage, x, y)
}

图形在名称空间
系统中。绘图
基本上我在我们的一个应用程序中使用它:
我们希望在视频帧上覆盖播放图标:

Image playbutton;
try
{
    playbutton = Image.FromFile(/*somekindofpath*/);
}
catch (Exception ex)
{
    return;
}

Image frame;
try
{
    frame = Image.FromFile(/*somekindofpath*/);
}
catch (Exception ex)
{
    return;
}

using (frame)
{
    using (var bitmap = new Bitmap(width, height))
    {
        using (var canvas = Graphics.FromImage(bitmap))
        {
            canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
            canvas.DrawImage(frame,
                             new Rectangle(0,
                                           0,
                                           width,
                                           height),
                             new Rectangle(0,
                                           0,
                                           frame.Width,
                                           frame.Height),
                             GraphicsUnit.Pixel);
            canvas.DrawImage(playbutton,
                             (bitmap.Width / 2) - (playbutton.Width / 2),
                             (bitmap.Height / 2) - (playbutton.Height / 2));
            canvas.Save();
        }
        try
        {
            bitmap.Save(/*somekindofpath*/,
                        System.Drawing.Imaging.ImageFormat.Jpeg);
        }
        catch (Exception ex) { }
    }
}

在这一切之后,我找到了一个新的更简单的方法尝试这个

它可以将多张照片连接在一起:

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 ex)
    {
        if (finalImage != null)
            finalImage.Dispose();

        throw ex;
    }
    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(); } } }
谢谢!完全救了我的命today@downvoter想详细说明一下吗,这样我就可以增强我的答案了?@Andreasniedermir,这个被否决的投票者可能复制粘贴了你的代码,但不起作用。这是一个黄金答案!工作得很好。g、 如果要合并动画spritesfinalImage=new System.Drawing.Bitmap(宽度、高度)的PNG图像,请清除(Color.Transparent);宽度的高值引发错误/height@Anant达比好的,很抱歉,我带回了一个老问题,但我将其转换为VB.NET。。如果下一张图像上未使用的像素/空白像素是透明的,那么如果我将它们放置在彼此上方,这会覆盖其他照片吗?如果没有,有什么办法吗?这有帮助吗?