簡單的想法:我有兩個圖像要合併,一個是500x500,中間透明,另一個是150x150。在C#/。NET中合併兩個圖像
基本想法是這樣的:創建一個500x500的空畫布,將150x150圖片放置在空畫布中間,然後將500x500圖片複製到透明中間,以使150x150能夠透過。
我知道如何在Java,PHP和Python中完成它......我只是不知道在C#中使用什麼對象/類,一個將圖像複製到另一個的快速示例就足夠了。
簡單的想法:我有兩個圖像要合併,一個是500x500,中間透明,另一個是150x150。在C#/。NET中合併兩個圖像
基本想法是這樣的:創建一個500x500的空畫布,將150x150圖片放置在空畫布中間,然後將500x500圖片複製到透明中間,以使150x150能夠透過。
我知道如何在Java,PHP和Python中完成它......我只是不知道在C#中使用什麼對象/類,一個將圖像複製到另一個的快速示例就足夠了。
基本上我用這個在我們的應用程序之一:我們要覆蓋一個playicon在視頻的幀 :
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) { }
}
}
謝謝!今天完全保存我的培根 – 2011-05-05 22:26:48
@downvoter謹慎詳細說明,以便我可以加強我的答案? – 2014-11-18 13:21:57
這會將圖像添加到另一個圖像。
using (Graphics grfx = Graphics.FromImage(image))
{
grfx.DrawImage(newImage, x, y)
}
圖形是在命名空間System.Drawing
畢竟這個,我發現了一個新的更簡單的方法試試這個..
它可以連接多張照片在一起:
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();
}
}
}
這有幫助嗎? http://www.daniweb.com/forums/thread87993.html – Dror 2014-05-01 23:25:24