2016-05-17 74 views
1

我需要绘制了在PictureBox加载的位图加载的位图。我一直在寻找一种方式在这个网站上,但答案不工作,因为产生的图像是在一个错误的位置。上绘制在PictureBox

private void pbImage_MouseClick(object sender, MouseEventArgs e) 
{ 
    currentImage = pbImage.Image; 
    Bitmap imageAux = new Bitmap(currentImage.Width, currentImage.Height); 
    using (Graphics g = Graphics.FromImage(imageAux)) 
    { 
     Brush brush = new SolidBrush(Color.Red); 
     Pen pen = new Pen(Color.Black, 10); 
     g.DrawImage(currentImage, pbImage.Location); 
     g.Flush(); 
     if (e.Button == MouseButtons.Left) 
     { 
      Control control = (Control)sender; 
      punto = ((Control)sender).PointToScreen(new Point(e.X, e.Y)); ; 

      g.DrawRectangle(pen, new Rectangle(punto.X, punto.Y, 50, 50)); 
      g.FillRectangle(brush, new Rectangle(punto.X, punto.Y, 50, 50)); 
      g.Flush(); 
      g.Dispose(); 
      pbImage.Invalidate(); 
     } 
    } 
    pbImage.Refresh(); 
    currentImage.Save("C:\\prueba3.bmp"); 
    imageAux.Save("C:\\prueba4.bmp"); 
    pbImage.Image = (Image)imageAux.Clone(); 
} 
+2

'使用(Graphics g = Graphics.FromImage(imageAux))'和'g.Dispose();'不应该组合。它是一个或另一个。 –

+0

使用'0,0'坐标代替'pbImage.Location'。 –

+0

但是这会将图像绘制到绘图箱位置?谢谢你!! – Neus

回答

0

你应该创建额外的位图绘制。

  • 将原稿中的位图。
  • 创建位图分配给PictureBox的。 (副刊)
  • 创建/生成原始位的灰度版本。

当需要借鉴:

  • 清除PictureBox的位图
  • 绘制灰阶到
  • 绘制50,50 RECT原来的位置选择在PictureBox的位图
  • 刷新het图片盒

这里是一个例子,添加一个pi将cturebox添加到表单中,并用您的图像文件名替换文件名。

public partial class Form1 : Form 
{ 
    private Bitmap _originalBitmap; 
    private Bitmap _grayScaleBitmap; 
    private Bitmap _pictureBoxBitmap; 

    //private Brush brush = new SolidBrush(Color.Red); 
    private Pen pen = new Pen(Color.Black, 1); 
    private Point? _inspectRectStart; 

    public Form1() 
    { 
     InitializeComponent(); 

     _originalBitmap = (Bitmap)Bitmap.FromFile("lin-png-256x256-Rafael_256x256_png-256x256.png"); 
     _grayScaleBitmap = MakeGrayscale3(_originalBitmap); 

     _pictureBoxBitmap = new Bitmap(_originalBitmap.Width, _originalBitmap.Height); 
     DrawImage(); 

     pictureBox1.Image = _pictureBoxBitmap; 
    } 

    private static Bitmap MakeGrayscale3(Bitmap original) 
    { 
     //create a blank bitmap the same size as original 
     Bitmap newBitmap = new Bitmap(original.Width, original.Height); 

     //get a graphics object from the new image 
     Graphics g = Graphics.FromImage(newBitmap); 

     //create the grayscale ColorMatrix 
     ColorMatrix colorMatrix = new ColorMatrix(
      new float[][] 
      { 
      new float[] {.3f, .3f, .3f, 0, 0}, 
      new float[] {.59f, .59f, .59f, 0, 0}, 
      new float[] {.11f, .11f, .11f, 0, 0}, 
      new float[] {0, 0, 0, 1, 0}, 
      new float[] {0, 0, 0, 0, 1} 
      }); 

     //create some image attributes 
     ImageAttributes attributes = new ImageAttributes(); 

     //set the color matrix attribute 
     attributes.SetColorMatrix(colorMatrix); 

     //draw the original image on the new image 
     //using the grayscale color matrix 
     g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height), 
      0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes); 

     //dispose the Graphics object 
     g.Dispose(); 
     return newBitmap; 

    } 
    private void DrawImage() 
    { 
     using (Graphics g = Graphics.FromImage(_pictureBoxBitmap)) 
     { 
      g.Clear(Color.White); // if the bitmap has transparent parts 

      // draw the grayscale image. 
      g.DrawImage(_grayScaleBitmap, new Rectangle(0, 0, _originalBitmap.Width, _originalBitmap.Height), 0, 0, _originalBitmap.Width, _originalBitmap.Height, GraphicsUnit.Pixel); 

      // if there is a selection, draw it. 
      if (_inspectRectStart.HasValue) 
      { 
       var rect = new Rectangle(_inspectRectStart.Value, new Size(50, 50)); 

       // draw the original bitmap in a rectangle. 
       g.DrawImage(_originalBitmap, _inspectRectStart.Value.X, _inspectRectStart.Value.Y, rect, GraphicsUnit.Pixel); 
       g.DrawRectangle(pen, rect); 
      } 
     } 

     pictureBox1.Refresh(); 
    } 

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e) 
    { 
     // move the rectangle position 
     _inspectRectStart = e.Location; 
     // redraw image. 
     DrawImage(); 
    } 

} 
+0

谢谢你的时间...我有一个提示,我在图片框中加载的位图是可变的。我的意思是,窗体有一些控件(组合框),并根据选择,来自图片框的propierty图像从数据库或目录中读取。是否需要初始化组件? – Neus

+0

您不应该直接将图片/位图分配给图片框。您应该将图像加载/绘制到'_originalBitmap'中 –

0

我想,那是因为g.DrawImage(currentImage, pbImage.Location);将关系画上了imageAux图像相对与pbImage的实际位置到它的父。改为尝试new Point();

+0

我曾尝试和图像是在正确的位置,但我看不到的矩形抽奖:S谢谢! – Neus