• 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();
    哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)成功.---Geovin Du(涂聚文)
  • 相关阅读:
    线程状态
    线程的两种实现方式
    设计模式
    移动端适配总结(补充一个移动端touch案例)
    echart.js组件编写
    vuex,文件夹整理
    rem初始化
    PC初始化
    移动端初始化
    三角形实现原理
  • 原文地址:https://www.cnblogs.com/geovindu/p/2846538.html
Copyright © 2020-2023  润新知