控件中国网现已改版,您看到的是老版本网站的镜像,系统正在为您跳转到新网站首页,请稍候.......
中国最专业的商业控件资讯网产品咨询电话:023-67870900 023-67871946
产品咨询EMAIL:SALES@COMPONENTCN.COM

按钮的背景图

作者:佚名 出处:互联网 2011年09月14日 阅读:

按钮的背景图

 private void pictureBox1_MouseEnter(object sender, EventArgs e)
        {
            pictureBox1.Image = GetImageByIndex(DefImage, 1);
        }
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            pictureBox1.Image = GetImageByIndex(DefImage, 2);
        }
        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            pictureBox1.Image = GetImageByIndex(DefImage, 1);
        }

        private void pictureBox1_MouseLeave(object sender, EventArgs e)
        {
            pictureBox1.Image = GetImageByIndex(DefImage, 0);
        }

        private Bitmap DefImage;

        private void Form1_Load(object sender, EventArgs e)
        {
            DefImage = pictureBox1.Image as Bitmap;

            pictureBox1.Image = GetImageByIndex(DefImage, 0);


        }
        private float _BgImageCount = 3;
        public float BgImageCount
        {
            get { return _BgImageCount; }
            set { _BgImageCount = value; }
        }

        /// <summary>
        /// 通过索引获取裁剪后的图片,默认以水平获取
        /// </summary>
        /// <param name="bitmap">原图</param>
        /// <param name="index">要获取的索引</param>
        public Image GetImageByIndex(Bitmap bitmap, int index)
        {
            return GetImageByIndex(bitmap, index, true);
        }
        /// <summary>
        /// 通过索引获取裁剪后的图片,
        /// </summary>
        /// <param name="bitmap"></param>
        /// <param name="index"></param>
        /// <param name="IsHorizontal">获取方式,以水平获取传True 否则传False</param>
        public Image GetImageByIndex(Bitmap bitmap, int index, bool IsHorizontal)
        {
            return GetImageByIndex(bitmap, index, true, BgImageCount);

        }
        /// <summary>
        /// 通过索引获取裁剪后的图片,
        /// </summary>
        /// <param name="bitmap"></param>
        /// <param name="index"></param>
        /// <param name="IsHorizontal">获取方式,以水平获取传True 否则传False</param>
        /// <param name="bgImageCount">背景的个数</param>
        public Image GetImageByIndex(Bitmap bitmap, int index, bool IsHorizontal, float bgImageCount)
        {
            Image img = bitmap;
            float width, height;
            if (IsHorizontal)
            {
                width = bitmap.Width / bgImageCount;
                height = bitmap.Height;
                img = ImageCut(bitmap, index * width, 0, width, height);
            }
            else
            {
                width = bitmap.Width;
                height = bitmap.Height / bgImageCount;
                img = ImageCut(bitmap, 0, index * height, width, height);
            }
            return img;

        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="bitmap"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public Image ImageCut(Bitmap bitmap, float x, float y, float width, float height)
        {
            if (x + width > bitmap.Width)
            {
                width = bitmap.Width - x;
            }
            if (width <= 0)
            {
                width = bitmap.Width;
                x = 0;
            }
            if (y + height > bitmap.Height)
            {
                height = bitmap.Height - y;
                y = 0;
            }
            if (height <= 0)
            {
                height = bitmap.Height;
            }
            RectangleF rect = new RectangleF(x, y, width, height);
            PixelFormat format = bitmap.PixelFormat;
            Bitmap result = bitmap.Clone(rect, format);

            return result;
        }

热推产品

  • ActiveReport... 强大的.NET报表设计、浏览、打印、转换控件,可以同时用于WindowsForms谀坔攀戀Forms平台下......
  • AnyChart AnyChart使你可以创建出绚丽的交互式的Flash和HTML5的图表和仪表控件。可以用于仪表盘的创......
首页 | 新闻中心 | 产品中心 | 技术文档 | 友情连接 | 关于磐岩 | 技术支持中心 | 联系我们 | 帮助中心 Copyright-2006 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 电话:023 - 67870900 传真:023 - 67870270 产品咨询:sales@componentcn.com 渝ICP备12000264号 法律顾问:元炳律师事务所 重庆市江北区塔坪36号维丰创意绿苑A座28-5 邮编:400020
在线客服
在线客服系统
在线客服
在线客服系统