在 C#/.NET 中合并两个图像 [英] Merging two images in C#/.NET

查看:31
本文介绍了在 C#/.NET 中合并两个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单的想法:我有两张图片要合并,一张是 500x500,中间是透明的,另一张是 150x150.

Simple idea: I have two images that I want to merge, one is 500x500 that is transparent in the middle the other one is 150x150.

基本思路是:创建一个 500x500 的空画布,将 150x150 的图像放置在空画布的中间,然后将 500x500 的图像复制过来,使其中间的透明可以让 150x150 的图像透过.

Basic idea is this: Create an empty canvas that is 500x500, position the 150x150 image in the middle of the empty canvas and then copy the 500x500 image over so that the transparent middle of it allows the 150x150 to shine through.

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

I know how to do it in Java, PHP and Python... I just don't have any idea what objects/classes to use in C#, a quick example of copying an images into another would suffice.

推荐答案

基本上我在我们的一个应用程序中使用它:我们想在视频的帧上覆盖一个播放图标:

basically i use this in one of our apps: we want to overlay a playicon over a frame of a video:

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) { }
    }
}

这篇关于在 C#/.NET 中合并两个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆