我想用C#在picturebox内画图,发现失败。就是直线画在picturebox外的部分能正常显示,而在picturebox内的部分被遮住。我的picturebox背景色必须是白色,不能改。请问如何解决?

解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;namespace draw1
    {
        public partial class Form1 : Form
        {
            bool bDraw;
            public Form1()
            {
                InitializeComponent();
            }
            protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);
                PointF startPointF=new PointF(20,50);
                PointF endPointF=new PointF(300,50);
                if (bDraw)
                {
                    Graphics g = e.Graphics;
                    Pen p = new Pen(Color.Blue, 2);
                    g.DrawLine(p, startPointF, endPointF);
                    g.Dispose();
                }
            } 
            private void btnDraw_Click(object sender, EventArgs e)
            {
                this.Invalidate();
                bDraw = true;
            }
            private void Form1_Load(object sender, EventArgs e)
            {        }
        }
    }
    namespace draw1
    {
        partial class Form1
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;        /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }        #region Windows Form Designer generated code        /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.btnDraw = new System.Windows.Forms.Button();
                this.pbImg = new System.Windows.Forms.PictureBox();
                ((System.ComponentModel.ISupportInitialize)(this.pbImg)).BeginInit();
                this.SuspendLayout();
                // 
                // btnDraw
                // 
                this.btnDraw.Location = new System.Drawing.Point(192, 260);
                this.btnDraw.Name = "btnDraw";
                this.btnDraw.Size = new System.Drawing.Size(79, 26);
                this.btnDraw.TabIndex = 0;
                this.btnDraw.Text = "Draw";
                this.btnDraw.UseVisualStyleBackColor = true;
                this.btnDraw.Click += new System.EventHandler(this.btnDraw_Click);
                // 
                // pbImg
                // 
                this.pbImg.BackColor = System.Drawing.Color.White;
                this.pbImg.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                this.pbImg.Location = new System.Drawing.Point(112, 12);
                this.pbImg.Name = "pbImg";
                this.pbImg.Size = new System.Drawing.Size(363, 215);
                this.pbImg.TabIndex = 1;
                this.pbImg.TabStop = false;
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(511, 316);
                this.Controls.Add(this.pbImg);
                this.Controls.Add(this.btnDraw);
                this.Name = "Form1";
                this.Text = "Form1";
                this.Load += new System.EventHandler(this.Form1_Load);
                ((System.ComponentModel.ISupportInitialize)(this.pbImg)).EndInit();
                this.ResumeLayout(false);        }        #endregion        private System.Windows.Forms.Button btnDraw;
            private System.Windows.Forms.PictureBox pbImg;
        }
    }