Thursday, July 30, 2009

C# help me!!!?

Hello everybody.


I need some C# code lines to create a screen capture in Visual Studio C#.


With my project, I’m not want to capture all the screen, but I want to capture a partial area of my screen.





I can do this:


bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width... Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);


gfxScreenshot = Graphics.FromImage(bmpScreenshot);


gfxScreenshot.CopyFromScreen(Screen.Prim... Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);





gfxScreenshot.InterpolationMode = System.Drawing.Drawing2D.InterpolationMo...


bmpScreenshot.SetResolution(300, 300);


pictureBox1.Image = bmpScreenshot;


this.Show();


timer1.Stop();


button2.Enabled = true;


this.Text = "Schermo catturato con successo!!!!";





My problem is that I want to ca

C# help me!!!?
private void mouse_Move(object sender, MouseEventArgs e)


{





//Resize (actually delete then re-draw) the rectangle if the left


mouse button is held down


if (LeftButtonDown)


{





//Erase the previous rectangle


g.DrawRectangle(EraserPen, CurrentTopLeft.X, CurrentTopLeft.Y,


CurrentBottomRight.X - CurrentTopLeft.X, CurrentBottomRight.Y -


CurrentTopLeft.Y);





//Calculate X Coordinates


if (Cursor.Position.X %26lt; ClickPoint.X)


{


CurrentTopLeft.X = Cursor.Position.X;


CurrentBottomRight.X = ClickPoint.X;


}


else


{


CurrentTopLeft.X = ClickPoint.X;


CurrentBottomRight.X = Cursor.Position.X;


}





//Calculate Y Coordinates


if (Cursor.Position.Y %26lt; ClickPoint.Y)


{


CurrentTopLeft.Y = Cursor.Position.Y;


CurrentBottomRight.Y = ClickPoint.Y;


}


else


{


CurrentTopLeft.Y = ClickPoint.Y;


CurrentBottomRight.Y = Cursor.Position.Y;


}





//Draw a new rectangle


g.DrawRectangle(MyPen, CurrentTopLeft.X, CurrentTopLeft.Y,


CurrentBottomRight.X - CurrentTopLeft.X, CurrentBottomRight.Y -


CurrentTopLeft.Y);





}





}





Point StartPoint = new Point(ClickPoint.X, ClickPoint.Y);


Rectangle bounds = new Rectangle(ClickPoint.X, ClickPoint.Y,


CurrentPoint.X - ClickPoint.X, CurrentPoint.Y - ClickPoint.Y);


ScreenShot.CaptureImage(StartPoint, Point.Empty, bounds, ScreenPath);








class ScreenShot


{


public static void CaptureImage(Point SourcePoint, Point DestinationPoint,


Rectangle SelectionRectangle, string FilePath)


{


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.Bmp);


}


}


}
Reply:P.S. === HOW OLD ARE YOU? Report It

Reply:I rewrote some of your code to use a Rectangle for what to capture.





Rectangle screenshotBounds = Screen.PrimaryScreen.Bounds;


screenshotBounds.Inflate(-50, -50);


bmpScreenshot = new Bitmap( screenshotBounds.Width, screenshotBounds.Height);


gfxScreenshot = Graphics.FromImage( bmpScreenshot);


gfxScreenshot.CopyFromScreen( screenshotBounds.Location, new Point(0, 0), screenshotBounds.Size, CopyPixelOperation.SourceCopy );





I just made it inflate the bounds by -50, -50, which produces a capture of everything except for 50 pixels from each edge of the PrimaryScreen.Bounds. You can set screenshotBounds to any Rectangle and it should capture that only inside of bmpScreenshot.


No comments:

Post a Comment