图片保存为bmp

问题描述:

我有这样的代码:图片保存为bmp

Dictionary<int, Class1> MyDict = new Dictionary<int, Class1>(); 

我得到鼠标的位置

private void panel1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) 
    {    Mycl.X1 = e.X; 
        Mycl.X2 = e.Y;} 
private void panel1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) 
    { 
        Mycl.X3 = e.X; 
        Mycl.X4 = e.Y; 
     MyDict.Add(MyDict.Count + 1, Mycl); 
     panel1.Invalidate(); 
     Mycl = new Class1(); 
    } 
private void panel1_Paint(object sender, PaintEventArgs e) 
    { 
     if (panel1.BackgroundImage == null) 
     { 
      panel1.BackgroundImage = new Bitmap(this.Width, this.Height); 
     } 
     Pen MyPen = new Pen(Color.Black); 
     Graphics G = Graphics.FromImage(panel1.BackgroundImage); 
     foreach (KeyValuePair<int, Class1> kvp in MyDict) 
     { 
      Point pl1 = new Point(MyDict[kvp.Key].X1, MyDict[kvp.Key].X2); 
      Point pl2 = new Point(MyDict[kvp.Key].X3, MyDict[kvp.Key].X4); 
      //e.Graphics.DrawLine(MyPen, pl1, pl2); 
      G.DrawLine(MyPen, pl1, pl2); 
     } 
    } 

但是当我画这幅画,我有麻烦了。

当我使用e.graphics.drawline()线已经引起

G.drawline()没有线。

最后,我需要保存我的照片,但用e.drawline我有一张黑色的照片。

+0

那么你想要在面板上画点? – wonko79

+0

是的,但是使用G.drawline()则没有分数。 –

试试这个... note我还为panel1添加了一个MouseMove()处理程序:

public partial class Form1 : Form 
{ 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private class Class1 
    { 
     public int X1, X2, X3, X4; 
    } 

    private Class1 Mycl = new Class1(); 
    private Dictionary<int, Class1> MyDict = new Dictionary<int, Class1>(); 

    private void panel1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) 
    { 
     if (e.Button == System.Windows.Forms.MouseButtons.Left) 
     { 
      Mycl.X1 = e.X; 
      Mycl.X2 = e.Y; 
      Mycl.X3 = e.X; 
      Mycl.X4 = e.Y; 

      Point pt1 = panel1.PointToScreen(new Point(Mycl.X1, Mycl.X2)); 
      Point pt2 = panel1.PointToScreen(new Point(Mycl.X3, Mycl.X4)); 
      ControlPaint.DrawReversibleLine(pt1, pt2, panel1.BackColor); 
     } 
    } 

    private void panel1_MouseMove(object sender, MouseEventArgs e) 
    { 
     if (e.Button == System.Windows.Forms.MouseButtons.Left) 
     { 
      Point pt1 = panel1.PointToScreen(new Point(Mycl.X1, Mycl.X2)); 
      Point pt2 = panel1.PointToScreen(new Point(Mycl.X3, Mycl.X4)); 
      ControlPaint.DrawReversibleLine(pt1, pt2, panel1.BackColor); 

      Mycl.X3 = e.X; 
      Mycl.X4 = e.Y; 
      pt2 = panel1.PointToScreen(new Point(Mycl.X3, Mycl.X4)); 
      ControlPaint.DrawReversibleLine(pt1, pt2, panel1.BackColor); 
     } 
    } 

    private void panel1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) 
    { 
     if (e.Button == System.Windows.Forms.MouseButtons.Left) 
     { 
      Mycl.X3 = e.X; 
      Mycl.X4 = e.Y; 
      MyDict.Add(MyDict.Count + 1, Mycl); 
      panel1.Invalidate(); 
      Mycl = new Class1(); 
     } 
    } 

    private void panel1_Paint(object sender, PaintEventArgs e) 
    { 
     using (Pen MyPen = new Pen(Color.Black)) 
     { 
      foreach (KeyValuePair<int, Class1> kvp in MyDict) 
      { 
       Point pl1 = new Point(kvp.Value.X1, kvp.Value.X2); 
       Point pl2 = new Point(kvp.Value.X3, kvp.Value.X4); 
       e.Graphics.DrawLine(MyPen, pl1, pl2); 
      } 
     } 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     Bitmap bmp = new Bitmap(panel1.Width, panel1.Height); 
     panel1.DrawToBitmap(bmp, new Rectangle(new Point(0,0), panel1.Size)); 
     bmp.Save(@"C:\Users\Mike\Pictures\SomeImage.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); 
    } 

} 
+0

它是完美的!它的工作!非常感谢你!现在,我坐着,理解))) –

+0

是否可以在ControlPaint中绘制椭圆?我总是有一个rechtangle! –

+0

您需要更强大的设计和方法来处理其他形状。 –

你需要你的屏幕上的图形和你的位图对象的图形来区分...

这里有一个例子:

using System; 
using System.Drawing; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     private Button button1; 
     private PictureBox pictureBox1; 
     private Bitmap bmp; 
     private Point? p1 = null; 


     public Form1() 
     { 
      InitializeComponent(); 
      bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height); 

      using (Graphics g = Graphics.FromImage(bmp)) 
      { 
       g.FillRectangle(Brushes.White, 0, 0, bmp.Width, bmp.Height); 
      } 

      pictureBox1.Image = bmp; 
     } 

     private void InitializeComponent() 
     { 
      this.pictureBox1 = new System.Windows.Forms.PictureBox(); 
      this.button1 = new System.Windows.Forms.Button(); 
      ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); 
      this.SuspendLayout(); 
      // 
      // pictureBox1 
      // 
      this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill; 
      this.pictureBox1.Location = new System.Drawing.Point(0, 23); 
      this.pictureBox1.Name = "pictureBox1"; 
      this.pictureBox1.Size = new System.Drawing.Size(477, 352); 
      this.pictureBox1.TabIndex = 0; 
      this.pictureBox1.TabStop = false; 
      this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint); 
      this.pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown); 
      this.pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove); 
      this.pictureBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseUp); 
      // 
      // button1 
      // 
      this.button1.Dock = System.Windows.Forms.DockStyle.Top; 
      this.button1.Location = new System.Drawing.Point(0, 0); 
      this.button1.Name = "button1"; 
      this.button1.Size = new System.Drawing.Size(477, 23); 
      this.button1.TabIndex = 1; 
      this.button1.Text = "write bitmap to file"; 
      this.button1.UseVisualStyleBackColor = true; 
      this.button1.Click += new System.EventHandler(this.button1_Click); 
      // 
      // Form1 
      // 
      this.ClientSize = new System.Drawing.Size(477, 375); 
      this.Controls.Add(this.pictureBox1); 
      this.Controls.Add(this.button1); 
      this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; 
      this.Name = "Form1"; 
      ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); 
      this.ResumeLayout(false); 

     } 



     private void pictureBox1_MouseDown(object sender, MouseEventArgs e) 
     { 
      p1 = pictureBox1.PointToClient(MousePosition); 
     } 

     private void pictureBox1_MouseUp(object sender, MouseEventArgs e) 
     { 
      Point p2 = pictureBox1.PointToClient(MousePosition); 
      using (Graphics g = Graphics.FromImage(bmp)) 
      { 
       g.DrawLine(Pens.Black, p1.Value, pictureBox1.PointToClient(MousePosition)); 
      } 
      p1 = null; 
      pictureBox1.Invalidate(); 
     } 

     private void pictureBox1_Paint(object sender, PaintEventArgs e) 
     { 
      if (p1 != null) 
      { 
       e.Graphics.DrawLine(Pens.Black, p1.Value, pictureBox1.PointToClient(MousePosition)); 
      } 
     } 

     private void pictureBox1_MouseMove(object sender, MouseEventArgs e) 
     { 
      pictureBox1.Invalidate(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      var dialog=new SaveFileDialog(); 
      dialog.DefaultExt = "bmp"; 
      dialog.ValidateNames = true; 
      dialog.Filter = "Bitmap|*.bmp"; 
      dialog.AddExtension = true; 
      dialog.OverwritePrompt=true; 
      var result=dialog.ShowDialog(this); 
      if (result == System.Windows.Forms.DialogResult.OK) 
      { 
       using (var fs = dialog.OpenFile()) 
       { 
        bmp.Save(fs, System.Drawing.Imaging.ImageFormat.Bmp); 
       } 
      } 
     } 


    } 
} 

在例子中,你可以看到2个不同的DrawLine(...)调用...在画面绘画中的绘画事件句柄中的一个,它不会保留在位图中...鼠标中的一个处理位图中的修改...

+0

谢谢,我会理解,但你的代码很难.. –

+0

尝试看到它这样:用户点击和P1设置...现在用鼠标移动的图片框失效,因此它将重绘...一旦重绘它从屏幕上画一条从p1到鼠标的当前位置的线......一旦鼠标被释放,从p1到鼠标的当前位置的一条线被绘制到BITMAP并且p1被重置为零 – DarkSquirrel42