WF 绘制图片的部分

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/SANYUNI/article/details/46279761

前段时间要自定义控件  用WF做一个Slider  背景是用贴图的  因此为了显示出好的效果 就需要贴图  通过调节值来控制图片的显示部分

在这里做了一个垂直显示的控制  运行效果如下:



后台处理代码如下:

  public partial class Form1 : Form
    {
        int DrawHeight = 0;
        Image DrawImage = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory+"drawImage.jpg");
        public Form1()
        {
            InitializeComponent();
        }




        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            Rectangle draw1 = new Rectangle(250,DrawHeight,60,500-DrawHeight);
            g.DrawImage(DrawImage, draw1, new Rectangle(0, DrawHeight * DrawImage.Height / 500, DrawImage.Width, (500 - DrawHeight) * DrawImage.Height / 500),GraphicsUnit.Pixel);
        }
        private void trackBar1_ValueChanged(object sender, EventArgs e)
        {
            DrawHeight = ((TrackBar)sender).Value;
            textBox1.Text = DrawHeight.ToString();
            this.Invalidate();
        }


    }


猜你喜欢

转载自blog.csdn.net/SANYUNI/article/details/46279761