c#如何把图片背景处理成透明啊?

dangdang0883 2008-01-27 02:31:21
c#如何把图片背景处理成透明啊?
...全文
4150 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
kaiweidawang 2010-05-27
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 octverve 的回复:]
C# 多个图片叠加,图片透明.
下载该示例代码

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

using System.Drawing;
using System.Drawing.Imaging;
using System.Drawin……
[/Quote]

管用,bmWatermark.MakeTransparent();
ghl1984 2009-04-30
  • 打赏
  • 举报
回复
不错,就是要这样,哈哈谢谢了
wzuomin 2009-04-24
  • 打赏
  • 举报
回复
抠图啊
用PS也行,C#也能实现简单背景的抠图。
FatProgrammer 2009-04-24
  • 打赏
  • 举报
回复
怎么看不明白
mohugomohu 2008-01-29
  • 打赏
  • 举报
回复
我觉得用C#太麻烦.\还是用PhotoShop处理好保存成PNG格式直接导入比较好
octverve 2008-01-29
  • 打赏
  • 举报
回复
C# 多个图片叠加,图片透明.
下载该示例代码

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

namespace Haix.Utils
{
class GenerateImage
{
public struct favoriteImage
{
private string _imagePath;
private int _x;
private int _y;

public int x
{
get
{
return _x;
}
set
{
_x = value;
}
}

public int y
{
get
{
return _y;
}
set
{
_y = value;
}
}

public string imagePath
{
get
{
return _imagePath;
}
set
{
_imagePath = value;
}
}
}

[STAThread]
static void Main(string[] args)
{
string CurrentDirectory = System.Environment.CurrentDirectory;
string body_path=CurrentDirectory + "\\white.png";

favoriteImage[] FaImage = new favoriteImage[2];

FaImage[0].x = -3;
FaImage[0].y = 70;
FaImage[0].imagePath = CurrentDirectory + "\\1.png";

FaImage[1].x = 20;//65;
FaImage[1].y = -12;
FaImage[1].imagePath = CurrentDirectory + "\\2.png";

generateWinterMark(CurrentDirectory,body_path, FaImage);
}

/**//// <summary>
/// 生成水印
/// </summary>
/// <param name="Main">主图片路径,eg:body</param>
/// <param name="Child">要叠加的图片路径</param>
/// <param name="x">要叠加的图片位置的X坐标</param>
/// <param name="y">要叠加的图片位置的Y坐标</param>
/// <param name="isSave"></param>
/// <returns>生成图片的路径</returns>
private static string generateWinterMark(string savePath,string body_path,favoriteImage[] favorite)
{
//create a image object containing the photograph to watermark
Image imgPhoto = Image.FromFile(body_path);
int phWidth = imgPhoto.Width;
int phHeight = imgPhoto.Height;

//create a Bitmap the Size of the original photograph
Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);

//设置此 Bitmap 的分辨率。
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

//load the Bitmap into a Graphics object
Graphics grPhoto = Graphics.FromImage(bmPhoto);
//Set the rendering quality for this Graphics object
grPhoto.SmoothingMode = SmoothingMode.AntiAlias;//清除锯齿的呈现
//haix
for (int i = 0; i < favorite.Length; i++)
{
//Draws the photo Image object at original size to the graphics object.
grPhoto.DrawImage(
imgPhoto, // Photo Image object
new Rectangle(0, 0, phWidth, phHeight), // Rectangle structure
0, // x-coordinate of the portion of the source image to draw.
0, // y-coordinate of the portion of the source image to draw.
phWidth, // Width of the portion of the source image to draw.
phHeight, // Height of the portion of the source image to draw.
GraphicsUnit.Pixel); // Units of measure


//------------------------------------------------------------
//Step #2 - Insert Property image,For example:hair,skirt,shoes etc.
//------------------------------------------------------------
//create a image object containing the watermark
Image imgWatermark = new Bitmap(favorite[i].imagePath);
int wmWidth = imgWatermark.Width;
int wmHeight = imgWatermark.Height;


//Create a Bitmap based on the previously modified photograph Bitmap
Bitmap bmWatermark = new Bitmap(bmPhoto);
bmWatermark.MakeTransparent(); //使默认的透明颜色对此 Bitmap 透明。

//bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
//Load this Bitmap into a new Graphic Object
Graphics grWatermark = Graphics.FromImage(bmWatermark);


int xPosOfWm = favorite[i].x;
int yPosOfWm = favorite[i].y;

//叠加
grWatermark.DrawImage(imgWatermark,new Rectangle(xPosOfWm, yPosOfWm, wmWidth, wmHeight), //Set the detination Position
0, // x-coordinate of the portion of the source image to draw.
0, // y-coordinate of the portion of the source image to draw.
wmWidth, // Watermark Width
wmHeight, // Watermark Height
GraphicsUnit.Pixel, // Unit of measurment
null); //ImageAttributes Object


//Replace the original photgraphs bitmap with the new Bitmap
imgPhoto = bmWatermark;

//grWatermark.Dispose();
//imgWatermark.Dispose();
//grPhoto.Dispose();
//bmWatermark.Dispose();
}
//haix

string nowTime = DateTime.Now.Year.ToString()+DateTime.Now.Month.ToString()+DateTime.Now.Day.ToString();
nowTime += DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();

string saveImagePath = savePath + "\\FA" + nowTime + ".png";

//save new image to file system.
imgPhoto.Save(saveImagePath, ImageFormat.Png);
imgPhoto.Dispose();


return saveImagePath;
}
}
}


叠加后的图片:

一个body,一个hair,一个西服:





kaixin110 是一位Web工程师。他原来是在郑州一家软件公司做WebGIS (JSP+Servlet+Tomcat),对WebGIS的原理有一定研究,在狂热地编写代码之余,他喜欢打篮球并体验软件带给我们的惊喜。目前在中国深圳一公司做Web开发(ASP.NET),如果您希望就本文与 kaixin110联系,则可以通过kaixin110@gmail.com与他联系。
posted on 2007-08-03 15:07 kaixin110 阅读(444) 评论(3) 编辑 收藏 所属分类: Microsoft.NET


评论
#1楼 2007-08-03 15:40 flyingfish
截个图上来看看阿! 回复 引用 查看

#2楼 [TrackBack] 2007-08-19 20:58 将飞
http://www.cnblogs.com/kaixin110/archive/2007/08/03/841827.html
[引用提示]将飞引用了该文章, 地址: http://www.cnblogs.com/jfei1982/archive/2007/08/19/861895.html 回复 引用 查看

#3楼 [楼主] 2007-11-26 08:46 kaixin110
Bitmap bmWatermark = new Bitmap(bmPhoto);
bmWatermark.MakeTransparent(); //使默认的透明颜色对此 Bitmap 透明。

具体你可以调试code,然后自己看效果!


=======================================================
http://www.cnblogs.com/kaixin110/archive/2007/08/03/841827.html
dangdang0883 2008-01-29
  • 打赏
  • 举报
回复
好象是有个函数可以直接处理!
CGChina 2008-01-28
  • 打赏
  • 举报
回复
使用PNG格式。至于处理当然在Photoshop中了,如果不是动态图片。
james_gosling 2008-01-27
  • 打赏
  • 举报
回复
你先把图片添加到Form上 然后把Form调摄下透明 应该可以吧
tellmeiia 2008-01-27
  • 打赏
  • 举报
回复
不是件容易的事

float[][] ptsArray =
{
new float[] {1,0,0,0,0},
new float[] {0,1,0,0,0},
new float[] {0,0,1,0,0},
new float[] {0,0,0,0.5f,0},
new float[] {1,0,0,0,1}
};

其中的0.5f就是透明度为0.5

110,549

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧