Csharp: create Transparent Images in winform

 1 //透明背景图
 2            Brush b = new SolidBrush(Color.FromArgb(50, Color.Transparent));
 3 
 4  
 5            Bitmap image = new Bitmap(1015, 637);
 6            Graphics g = Graphics.FromImage(image);
 7            g.Clear(Color.Transparent);
 8            g.FillRectangle(b, 0, 0, 1015, 637);
 9            //g.DrawRectangle(Pens.Plum, 0, 0, 299, 49);
10            //Font font = new Font("Alba Super", 20, FontStyle.Underline);
11            //g.DrawString("This is a test.", font, Brushes.Plum, 10, 0);
12 
13            //System.Drawing.Image icon = System.Drawing.Image.FromFile(Server.MapPath("your.gif"));
14            //g.DrawImageUnscaled(icon, 240, 0);
15 
16            //image.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
17            //SolidBrush:定义单色画笔。画笔用于填充图形形状,如矩形、椭圆、扇形、多边形和封闭路径。
18            //这个画笔为描绘阴影的画笔,呈灰色
19            float alpha = 0;
20            int m_alpha = Convert.ToInt32(256 * alpha);
21            SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(m_alpha, 0, 0, 0));
22 
23            //从四个 ARGB 分量(alpha、红色、绿色和蓝色)值创建 Color 结构,这里设置透明度为153
24            //这个画笔为描绘正式文字的笔刷,呈白色
25            SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(153, 255, 255, 255));
26 
27 
28            g.DrawImage(image, 0, 0, image.Width, image.Height);
29            Font f = new Font(setFont, 18, FontStyle.Bold); //字體大小
30            Font fno = new Font(setFont, 10, FontStyle.Bold);
31            Font fclerk = new Font(setFont, 8, FontStyle.Bold);
32            Brush fb = new SolidBrush(Color.Black); //字體顏色                
33            string addText = "塗聚文";
34            //new RectangleF(0, 0, 500, 500), strFormat)  
35            g.DrawString(addText, f, fb, new RectangleF(130, 220, 550, 110), new StringFormat());//放的姓名位置
36            g.DrawString("行政及人事管理人員", fclerk, fb, new RectangleF(130, 410, 550, 110), new StringFormat());//放職稱的位置
37            g.DrawString("行政及人力资源管理部", fno, fb, new RectangleF(130, 470, 550, 110), new StringFormat());//放部門名稱的位置
38            g.DrawString("L00094", fno, fb, new RectangleF(130, 540, 550, 110), new StringFormat()); //放員工編號的位置
39 
40            int xPosOfWm;
41            int yPosOfWm;
42            int phWidth = image.Width;
43            int phHeight = image.Height;
44            Photopath = dirInfo + "2cun.jpg"; //2寸照片
45            System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Photopath);
46            int wmWidth = copyImage.Width;
47            int wmHeight = copyImage.Height;
48 
49 
50            xPosOfWm = phWidth - wmWidth - 80;
51            yPosOfWm = 80;
52            StringFormat strFormat = new StringFormat();
53 
54            g.DrawImage(copyImage, new Rectangle(xPosOfWm, yPosOfWm, wmWidth, wmHeight), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
55 
56            // Make backColor transparent for myBitmap.
57            ImageAttributes imgAttribs = new ImageAttributes();
58            imgAttribs.SetColorKey(Color.FromArgb(255, 255, 255), Color.FromArgb(255, 255, 255));
59            Color backColor = Color.Transparent;
60            image.MakeTransparent(backColor);
61            //image = MakeTransparentGif(image, Color.Transparent);
62            // Draw the transparent bitmap to the screen.
63            //g.DrawImage(image, image.Width, 0, image.Width, image.Height);
64            this.pictureBox1.Image = image;
65            image.Save(dirInfo + @"geovindu_1011.jpg", ImageFormat.Jpeg);
66 
67            g.Dispose();

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值