合并在C#两个图像/。NET [英] Merging two images in C#/.NET
问题描述
简单的想法:我有,我想合并两个图像,一个是500×500是透明的中间,另一种是150×150
基本思路是这样的:创建一个空白的画布,也就是500×500,在空白的画布的中间位置的150×150的图像,然后复制500×500的图像上,使得它的透明中间允许150×150,以彪炳。 P>
我知道如何做到这一点在Java中,PHP和Python ...我只是没有任何想法是什么对象/类在C#中,复制的图像转换成另一种就足够了一个简单的例子使用。
基本上我用这个在我们的应用程序之一: 我们要覆盖一个playicon在一个视频帧:
图片PLAYBUTTON;
尝试
{
PLAYBUTTON = Image.FromFile(/ * somekindofpath * /);
}
赶上(例外前)
{
返回
}
图像帧;
尝试
{
帧= Image.FromFile(/ * somekindofpath * /);
}
赶上(例外前)
{
返回
}
使用(帧)
{
使用(VAR位图=新位图(宽,高))
{
使用(VAR帆布= Graphics.FromImage(位图))
{
canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
canvas.DrawImage(帧,新的Rectangle(0,0,宽度,高度),新的Rectangle(0,0,frame.Width,frame.Height),GraphicsUnit.Pixel);
canvas.DrawImage(PLAYBUTTON,(bitmap.Width / 2) - (playbutton_width / 2 + 5),(bitmap.Height / 2) - (playbutton_height / 2 + 5));
canvas.Save();
}
尝试
{
bitmap.Save(/ * somekindofpath * /,ImageFormat.Jpeg);
}
赶上(例外前){}
}
}
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.
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.
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 + 5), (bitmap.Height / 2) - (playbutton_height / 2 + 5));
canvas.Save();
}
try
{
bitmap.Save(/*somekindofpath*/, ImageFormat.Jpeg);
}
catch (Exception ex) { }
}
}
这篇关于合并在C#两个图像/。NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!