2016-05-17 71 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