2011-06-21 111 views
1

所以,讓我開始了通過展示你的代碼我現在有:C#矩形圖和截圖

 private void Form1_MouseDown(object sender, MouseEventArgs e) 
    { 
     currentPos = startPos = e.Location; 
     drawing = true; 
    } 

    private void Form1_MouseMove(object sender, MouseEventArgs e) 
    { 
     currentPos = e.Location; 
     //Calculate X Coordinates 
     if (e.X < startPos.X) 
     { 
      CurrentTopLeft.X = e.X; 
     } 
     else 
     { 
      CurrentTopLeft.X = startPos.X; 
     } 

     //Calculate Y Coordinates 
     if (e.Y < startPos.Y) 
     { 
      CurrentTopLeft.Y = e.Y; 
     } 
     else 
     { 
      CurrentTopLeft.Y = startPos.Y; 
     } 

     if (drawing) 
      this.Invalidate(); 
    } 

    private void Form1_MouseUp(object sender, MouseEventArgs e) 
    { 
     if (drawing) 
     { 
      this.Hide(); 
      SaveScreen(); 
     } 
    } 

    private void Form1_Paint(object sender, PaintEventArgs e) 
    { 
     Color col = Color.FromArgb(75, 100, 100, 100); 
     SolidBrush b = new SolidBrush(col); 

     if (drawing) 
      e.Graphics.FillRectangle(b, getRectangle()); 
    } 

我SaveScreen功能:

 private void SaveScreen() 
     { 

     ScreenShot.CaptureImage(CurrentTopLeft, Point.Empty, getRectangle()); 

     } 

的CaptureImage功能:

 public static void CaptureImage(Point SourcePoint, Point DestinationPoint, Rectangle SelectionRectangle) 
    { 
     string FilePath = "temp.jpg"; 
     using (Bitmap bitmap = new Bitmap(SelectionRectangle.Width, SelectionRectangle.Height)) 
     { 
      using (Graphics g = Graphics.FromImage(bitmap)) 
      { 
       g.CopyFromScreen(SourcePoint, DestinationPoint, SelectionRectangle.Size); 
      } 
      bitmap.Save(FilePath, ImageFormat.Jpeg); 
     } 

     string Filename = String.Format("{0:yyyy-M-d-HH-mm-ss}", DateTime.Now) + ".jpg"; 
     string Server = ""; 

     System.Net.WebClient Client = new System.Net.WebClient(); 
     Client.Headers.Add("Content-Type", "image/jpeg"); 
     byte[] result = Client.UploadFile(Server + "upload.php?filename=" + Filename + "", "POST", FilePath); 
     string s = System.Text.Encoding.UTF8.GetString(result, 0, result.Length); 

     Program.mainForm.Notify(Server + Filename); 

     File.Delete(FilePath); 
    } 

這只是我在屏幕上繪製矩形的基本代碼。當矩形繪製時,它會拍攝一張圖像,效果很好。 問題是,矩形的繪製根本不平滑。我已經啓用了doublebuffering,幾乎嘗試了一切,但沒有運氣。

此外,我想抓住當前屏幕或凍結它,然後能夠在凍結屏幕上繪製,而不是在活動屏幕頂部繪製,如果您瞭解我的話。這將如何完成?

任何幫助非常感謝!

+0

你可能沒有在正確的分辨率繪製。 –

回答

0

你可以嘗試這樣的事:

int width = 
    Screen.PrimaryScreen.Bounds.Width, 
    height = Screen.PrimaryScreen.Bounds.Height; 

Bitmap screen = default(Bitmap); 

try 
{ 
    screen = new Bitmap 
    (
     width, 
     height, 
     Screen.PrimaryScreen.BitsPerPixel == 32 ? 
      PixelFormat.Format32bppRgb : 
       PixelFormat.Format16bppRgb565 
    ); 

    using (Graphics graphics = Graphics.FromImage(screen)) 
    { 
     graphics.SmoothingMode = SmoothingMode.AntiAlias; 

     graphics.CopyFromScreen 
     (
      new Point() { X = 0, Y = 0 }, 
      new Point() { X = 0, Y = 0 }, 
      new Size() { Width = width, Height = height }, 
      CopyPixelOperation.SourceCopy 
     ); 

     // Draw over the "capture" with Graphics object 
    } 
} 
finally 
{ 
    if (screen != null) 
    { 
     screen.Dispose(); 
    } 
} 
+0

我似乎無法得到那個工作。我可能只是空白,我將如何顯示屏幕作爲背景? – Lazze

+0

@Lazze作爲背景是什麼?你的表格?你有一個處理屏幕截圖的「圖形」對象,你可以使用http://www.pinvoke.net/default.aspx/gdi32.bitblt將它繪製到你的窗體上的「圖形」對象上,如果這是什麼您正在尋找? –

+0

是的,確切地說。我的表單開始於全屏,並準備好開始繪製。因此,我不想將其透明化,而是想要捕捉屏幕快照並以全屏顯示。 你有沒有可能給我一個關於如何將圖像繪製到我的表單上的例子? – Lazze