星空网 > 软件开发 > ASP.net

bitmap实现背景透明

  近日在项目中,一直被一个问题搞得头大的很,美工要把按钮图片弄成不规则的,但是在winform里实现又不仅仅是使用简单的png图片而已。在网上找到一些方法,稍微改了一点加工成项目所需。

贴出解决方案,以供日后使用:

 public class BitmapRegion  {    //创建支持位图区域的控件(目前有button,form,imagebutton)    public static void CreateControlRegion(Control control, Bitmap bitmap)    {      //判断控件是否存在      if (control == null )//|| bitmap == null        return;      //控件大小设置为位图大小      control.Width = bitmap.Width;      control.Height = bitmap.Height;      // 档控件为form时      if (control is System.Windows.Forms.Form)      {        //强制转化为form        Form form = (Form)control;        //当FORM的边界FormBorderStyle不为NONE时,应将FORM的大小设置成比位图大小稍大一点        form.Width += 15;        form.Height += 35;        //设置form为没有边界        form.FormBorderStyle = FormBorderStyle.None;        //将位图设置为控件背景图        form.BackgroundImage = bitmap;        //计算位图中不透明的部分        GraphicsPath graphicsPath = CalculateControlGraphicsPath(bitmap);        //应用新的区域        form.Region = new Region(graphicsPath);      }      //当控件是panel时      else if (control is System.Windows.Forms.Panel)      {        //强制转化为panel        Panel form = control as Panel;        //当FORM的边界FormBorderStyle不为NONE时,应将FORM的大小设置成比位图大小稍大一点        //form.Width += 15;        //form.Height += 35;        //form.Width += 15;        //form.Height += 35;        // 设置panel为没有边界        form.BorderStyle = BorderStyle.None;        //将位图设置为控件背景图        form.BackgroundImage = bitmap;        //计算位图中不透明的部分        GraphicsPath graphicsPath = CalculateControlGraphicsPath(bitmap);        //应用新的区域        form.Region = new Region(graphicsPath);      }      //当控件是button时      else if (control is System.Windows.Forms.Button)      {        // 强制转化为button        Button  button = (Button)control;                // 不显示button文字        button.Text = "";        // 改变cursor的style        button.Cursor = Cursors.Hand;        // 设置button的背景图片        button.BackgroundImage = bitmap;        // 计算图中不透明部分        GraphicsPath graphicsPath = CalculateControlGraphicsPath(bitmap);        // 应用新的区域        button.Region = new Region(graphicsPath);      }      //当控件是imagebutton时      else if (control is M3Host.view.utils.ImageButton)      {        M3Host.view.utils.ImageButton button = control as M3Host.view.utils.ImageButton;        // 不显示文字        button.Text = "";        // 改变模式和设置正常状态下的图片为空        button.Cursor = Cursors.Hand;        button.NormalImage = null;        // 设置位图为背景图片        button.BackgroundImage = bitmap;        // 计算图中不透明部分        GraphicsPath graphicsPath = CalculateControlGraphicsPath(bitmap);        // 应用新的区域        button.Region = new Region(graphicsPath);      }    }    // 计算位图中不透明部分    private static GraphicsPath CalculateControlGraphicsPath(Bitmap bitmap)    {      // 创建graphicsPath      GraphicsPath graphicsPath = new GraphicsPath();      // 取得左上角的第一个点作为透明点      Color colorTransparent = bitmap.GetPixel(0, 0);      // 第一个找到的点      int colOpaquePixel = 0;      // 遍历所有Y方向的点      for (int row = 0; row < bitmap.Height; row++)      {        // 重设        colOpaquePixel = 0;        // 遍历X方向的所有点        for (int col = 0; col < bitmap.Width; col++)        {          // 如果不是透明点,则继续遍历          if (bitmap.GetPixel(col, row) != colorTransparent)          {            // 记录当前点            colOpaquePixel = col;            // 新建变量记录当前点            int colNext = col;            // 从找到的不透明点开始,继续寻找不透明点,一直到找到或则达到图片宽度            for (colNext = colOpaquePixel; colNext < bitmap.Width; colNext++)              if (bitmap.GetPixel(colNext, row) == colorTransparent)                break;            // 将不透明点加到graphics path            graphicsPath.AddRectangle(new Rectangle(colOpaquePixel,                          row, colNext - colOpaquePixel, 1));            //覆盖前一个点            col = colNext;          }        }      }      return graphicsPath;    }  }

使用方法:Bitmap bmp = new Bitmap("images\\move1.bmp");
BitmapRegion.CreateControlRegion(控件名, bmp);

再附上将控件制成bitmap的方法

Bitmap bmp = new Bitmap(Width, Height);Rectangle r = new Rectangle(0, 0, Width,Height);//width和height均为控件的宽和高控件名.DrawToBitmap(bmp, r);

  记录以上。。




原标题:bitmap实现背景透明

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

大过年的:https://www.goluckyvip.com/tag/7106.html
爆款打造攻略:https://www.goluckyvip.com/tag/7107.html
大卖新品:https://www.goluckyvip.com/tag/7108.html
如何备战黑五:https://www.goluckyvip.com/tag/7109.html
或判50年:https://www.goluckyvip.com/tag/711.html
如何处理差评:https://www.goluckyvip.com/tag/7110.html
武陵山大裂谷周围景点 武陵山大裂谷周围景点图片:https://www.vstour.cn/a/411233.html
南美旅游报价(探索南美洲的旅行费用):https://www.vstour.cn/a/411234.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流