Csharp: create Transparent Images in winform

//透明背景图
           Brush b = new SolidBrush(Color.FromArgb(50, Color.Transparent));

 
           Bitmap image = new Bitmap(1015, 637);
           Graphics g = Graphics.FromImage(image);
           g.Clear(Color.Transparent);
           g.FillRectangle(b, 0, 0, 1015, 637);
           //g.DrawRectangle(Pens.Plum, 0, 0, 299, 49);
           //Font font = new Font("Alba Super", 20, FontStyle.Underline);
           //g.DrawString("This is a test.", font, Brushes.Plum, 10, 0);

           //System.Drawing.Image icon = System.Drawing.Image.FromFile(Server.MapPath("your.gif"));
           //g.DrawImageUnscaled(icon, 240, 0);

           //image.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
           //SolidBrush:定义单色画笔。画笔用于填充图形形状,如矩形、椭圆、扇形、多边形和封闭路径。
           //这个画笔为描绘阴影的画笔,呈灰色
           float alpha = 0;
           int m_alpha = Convert.ToInt32(256 * alpha);
           SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(m_alpha, 0, 0, 0));

           //从四个 ARGB 分量(alpha、红色、绿色和蓝色)值创建 Color 结构,这里设置透明度为153
           //这个画笔为描绘正式文字的笔刷,呈白色
           SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(153, 255, 255, 255));


           g.DrawImage(image, 0, 0, image.Width, image.Height);
           Font f = new Font(setFont, 18, FontStyle.Bold); //字體大小
           Font fno = new Font(setFont, 10, FontStyle.Bold);
           Font fclerk = new Font(setFont, 8, FontStyle.Bold);
           Brush fb = new SolidBrush(Color.Black); //字體顏色                
           string addText = "塗聚文";
           //new RectangleF(0, 0, 500, 500), strFormat)  
           g.DrawString(addText, f, fb, new RectangleF(130, 220, 550, 110), new StringFormat());//放的姓名位置
           g.DrawString("行政及人事管理人員", fclerk, fb, new RectangleF(130, 410, 550, 110), new StringFormat());//放職稱的位置
           g.DrawString("行政及人力资源管理部", fno, fb, new RectangleF(130, 470, 550, 110), new StringFormat());//放部門名稱的位置
           g.DrawString("L00094", fno, fb, new RectangleF(130, 540, 550, 110), new StringFormat()); //放員工編號的位置

           int xPosOfWm;
           int yPosOfWm;
           int phWidth = image.Width;
           int phHeight = image.Height;
           Photopath = dirInfo + "2cun.jpg"; //2寸照片
           System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Photopath);
           int wmWidth = copyImage.Width;
           int wmHeight = copyImage.Height;


           xPosOfWm = phWidth - wmWidth - 80;
           yPosOfWm = 80;
           StringFormat strFormat = new StringFormat();

           g.DrawImage(copyImage, new Rectangle(xPosOfWm, yPosOfWm, wmWidth, wmHeight), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);

           // Make backColor transparent for myBitmap.
           ImageAttributes imgAttribs = new ImageAttributes();
           imgAttribs.SetColorKey(Color.FromArgb(255, 255, 255), Color.FromArgb(255, 255, 255));
           Color backColor = Color.Transparent;
           image.MakeTransparent(backColor);
           //image = MakeTransparentGif(image, Color.Transparent);
           // Draw the transparent bitmap to the screen.
           //g.DrawImage(image, image.Width, 0, image.Width, image.Height);
           this.pictureBox1.Image = image;
           image.Save(dirInfo + @"geovindu_1011.jpg", ImageFormat.Jpeg);

           g.Dispose();

你可能感兴趣的:(Csharp: create Transparent Images in winform)