So, I'm trying to make simple screenshot app in Window Forms, but i would like my button to dissapear whenever I take a shot. Unfortunately, it does not want to be invisible, even after setting Visible to false:
{
InitializeComponent();
this.BackColor = Color.Red;
this.TransparencyKey = BackColor;
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
}
Image tmp = Image.FromFile("C:/Users/Bartek/source/repos/Hadr/Hadr/Image/image.png");
private void button1_Click_1(object sender, EventArgs e)
{
Bitmap bmp = new Bitmap(tmp.Width, tmp.Height);
Graphics g = Graphics.FromImage(bmp);
button1.Visible = false;
g.CopyFromScreen(0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size);
pictureBox1.Image = bmp;
bmp.Save("C:/Folder/image1.png",System.Drawing.Imaging.ImageFormat.Png);
}
I have set dock on picturebox to fill, and placed button in the way showed on screen:
I'm kindly asking for help
Hide the button first and then use BeginInvoke() to defer making the screenshot until the system updates controls, like this:
private void button1_Click_1(object sender, EventArgs e)
{
button1.Visible = false;
BeginInvoke(MakeScreenShot);
}
void MakeScreenshot()
{
Bitmap bmp = new Bitmap(tmp.Width, tmp.Height);
Graphics g = Graphics.FromImage(bmp);
g.CopyFromScreen(0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size);
pictureBox1.Image = bmp;
bmp.Save("C:/Folder/image1.png", System.Drawing.Imaging.ImageFormat.Png);
button1.Visible = true;
}
Related
I'm trying to print windows forms and it is printing well but the issue is I want to print the form to the whole page. As you guys can see the the end of the page is empty. Is there a way to stretch is or something as solution. I'm new to C# and windows forms and don't know much, kindly anyone help !!. Explanation which is easy to understand.
here is my code:
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawImage(bitmap, 0, 0);
}
private void Print_Click(object sender, EventArgs e)
{
Panel panel = new Panel();
this.Controls.Add(panel);
Graphics grp = panel.CreateGraphics();
Size formSize = this.ClientSize;
bitmap = new Bitmap(formSize.Width, formSize.Height, grp);
grp = Graphics.FromImage(bitmap);
Point panelLocation = PointToScreen(panel.Location);
grp.CopyFromScreen(panelLocation.X, panelLocation.Y, 0, 0, formSize);
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.PrintPreviewControl.Zoom = 1;
printPreviewDialog1.ShowDialog();
}
Bitmap bitmap;
private void CaptureScreen()
{
Graphics myGraphics = this.CreateGraphics();
Size s = this.Size;
bitmap = new Bitmap(s.Width, s.Height, myGraphics);
Graphics memoryGraphics = Graphics.FromImage(bitmap);
memoryGraphics.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, s);
}
I'm creating a software in which pictureBox2 updates with the color of the pixel clicked on pictureBox1.
Already tried to use refresh(), but pictureBox2 does not change.
private void PictureBox1_MouseUp(object sender, MouseEventArgs e)
{
Bitmap b = new Bitmap(pictureBox1.Image);
color = b.GetPixel(e.X, e.Y); // Color
solidColor = new Bitmap(pictureBox2.Width, pictureBox2.Height, PixelFormat.Format24bppRgb); //Image
using (Graphics grp = Graphics.FromImage(solidColor))
{
SolidBrush co = new SolidBrush(color);
grp.FillRectangle( co, 0, 0, pictureBox2.Width, pictureBox2.Height);
}
pictureBox2.Image = solidColor;
}
I was able to solve this by doing this.
private void PictureBox1_Click(object sender, EventArgs e)
{
Bitmap b = new Bitmap(pictureBox1.Image);
MouseEventArgs me = (MouseEventArgs)e;
Point cord= me.Location;
color = b.GetPixel(cord.X,cord.Y);
solidColor = new Bitmap(pictureBox2.Width, pictureBox2.Height, PixelFormat.Format24bppRgb);
using (Graphics grp = Graphics.FromImage(solidColor))
{
SolidBrush co = new SolidBrush(color);
grp.FillRectangle(co, 0, 0, pictureBox2.Width, pictureBox2.Height);
}
pictureBox2.Image = solidColor;
}
```c#
So when ever I hold the left mouse button and draw the rectangle on the form.
The form itself keeps flickering.
I've tried setting the DoubleBuffered property to true, that didnt change anything.
Is there a way to change the refresh rate or something similar?
Because from what I've tried, the example above and
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
It doesnt seem to change anything
Here is a gif showing the flickering
https://i.imgur.com/Wa3kmbM.gifv
private void Form1_Load(object sender, EventArgs e)
{
//Hide the Form
this.Hide();
label1.Visible = false;
label2.Visible = false;
//Create the Bitmap (This is an a black bitmap holding just the size.) (Bitmap is mutable)
Bitmap printscreen = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height);
//Create a graphics object that's ready for alteration. `printscreen` is now a graphics Object
Graphics graphics = Graphics.FromImage(printscreen);
//Alter the graphics object which again is the printscreen
graphics.CopyFromScreen(0, 0, 0, 0, printscreen.Size);
//Create a temporary memory stream for the image
using (MemoryStream s = new MemoryStream())
{
//save graphic variable into memory
printscreen.Save(s, ImageFormat.Bmp);
//Set the size of the picturebox
pictureBox1.Size = new Size(Width, Height);
//set the value of the picturebox.Image to an Image.FromStream and load the temp stream
pictureBox1.Image = Image.FromStream(s);
}
//Show Form
this.Show();
//Cross Cursor
Cursor = Cursors.Cross;
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
//startX is holds the corresponding value of where the mouse is in relation to the pixels of the width
//for instance, if it's all the way to the right, it holds the value of 1920.
startX = e.X;
startY = e.Y;
selectPen = new Pen(Color.DimGray, 2);
selectPen.DashStyle = DashStyle.Dash;
pictureBox1.Refresh();
start = true;
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (start)
{
pictureBox1.Refresh();
//selectingWidth is equal to how much we have selected from the starting point.
//Let's say the starting point is 10 and our current mouse position is at 10 then we have selected 0 pixels to the right.
//However if we move our mouse 10 pixels to the right it means we have selected 10 pixels to the right.
selectingWidth = e.X - startX;
selectingHeight = e.Y - startY;
//if the selectingWidth is less than 0, then set the origin to the current position of the mouse.
var drawfromX = selectingWidth < 0 ? e.X : startX;
var drawfromY = selectingHeight < 0 ? e.Y : startY;
pictureBox1.Refresh();
pictureBox1.CreateGraphics().DrawRectangle(selectPen,
drawfromX,
drawfromY,
Math.Abs(selectingWidth),
Math.Abs(selectingHeight));
}
}
I want to make a screenshot of a Panel on my Mainform. This screenshot should be made after the user chose some Options on a subform. At the beginning everything went fine but now the screenshot contains parts of the subform.
The subform gets opened like this:
private void Bexport_Click(object sender, EventArgs e) //button
{
ex = new Export();
initexForm();
ex.FormClosed += this.exFormClosed;
ex.TXTfilename.Focus();
ex.ShowDialog(this);
}
The function which makes the screenshot:
void exFormClosed(object sender, EventArgs e)
{
try
{
System.Drawing.Rectangle bounds = Mainpanel.Bounds;
bounds.Width = bounds.Width - 6;
bounds.Height = bounds.Height - 4;
using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
{
using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(
Mainpanel.PointToScreen(new Point()).X + 3,
Mainpanel.PointToScreen(new Point()).Y + 2, 0,
0, bounds.Size);
}
bitmap.Save(Application.StartupPath + temppic.bmp);
Document doc = new Document();
...
I used the events FormClosed and FormClosing, both with similar results. Then I tried to hide the subform with ex.Hide() but it hid the whole program, means the screenshot showed the desktop from behind the program.
Anybody an idea how to make sure that the subform is closed before making the screenshot?
Jonathan
The problem might be that the main form didn't have time to repaint after the subform closed.
this.Update();
will force the Form to repaint (http://msdn.microsoft.com/en-us/library/system.windows.forms.control.update.aspx)
What you have to do is create a dummy form that's the size of the control you want to draw then add the control to the dummy form and show the form and draw the control from the dummy.
public Bitmap ControlToBitmap(Control ctrl)
{
Bitmap image = new Bitmap(ctrl.Width, ctrl.Height);
//Create form
Form f = new Form();
//add control to the form
f.Controls.Add(ctrl);
//set the size of the form to the size of the control
f.Size = ctrl.Size;
//draw the control to the bitmap
ctrl.DrawToBitmap(image, new Rectangle(0, 0, ctrl.Width, ctrl.Height));
//dispose the form
f.Dispose();
return image;
}
So if you call it like this:
void exFormClosed(object sender, EventArgs e)
{
Bitmap bitmap ControlToBitmap(Mainpanel);
bitmap.Save(Application.StartupPath + temppic.bmp);
Document doc = new Document();
...
This will work even if the form has already been closed.
void exFormClosed(object sender, EventArgs e)
{
try
{
Application.DoEvents();
System.Drawing.Rectangle bounds = Mainpanel.Bounds;
bounds.Width = bounds.Width - 6;
bounds.Height = bounds.Height - 4;
using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
{
using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(
Mainpanel.PointToScreen(new Point()).X + 3,
Mainpanel.PointToScreen(new Point()).Y + 2,
0, 0, bounds.Size);
}
...
i need help on inserting a box which i drew out from into a picturebox.
here is the code of the pen that i code out, i do not know how to put it in the picturebox.
there will be a webcam running on the background of the picturebox, i want my rectangle to be inside the picturebox.
private void button1_Click(object sender, EventArgs e)
{
if (button1.Text == "Start")
{
Graphics myGraphics = base.CreateGraphics();
myGraphics.Clear(Color.White);
Pen myPen = new Pen(Color.DarkBlue);
Rectangle rect = new Rectangle(480, 70, 120, 120);
myGraphics.DrawRectangle(myPen, rect);
stopWebcam = false;
button1.Text = "Stop";
}
else
{
stopWebcam = true;
button1.Text = "Start";
}
}
Paining in winforms is primarily done in the OnPaint event. Your ButtonClick event handler should only setup the stage for OnPaint and possibly activate it. Example:
public class MyForm : Form
...
private Rectangle? _boxRectangle;
private void OnMyButtonClick(object sender, EventArgs e)
{
if (button1.Text == "Start")
{
_boxRectangle = new Rectangle(...);
button1.Text = "Stop";
}
else
{
_boxRectangle = null;
button1.Text = "Start";
}
Invalidate(); // repaint
}
protected override OnPaint(PaintEventArgs e)
{
if (_boxRectangle != null)
{
Graphics g = e.Graphics.
Pen pen = new Pen(Color.DarkBlue);
g.DrawRectangle(_boxRectangle);
}
}
}
You may have to draw the web cam image onto a bitmap buffer and use that as an image for the picture box.
Here is the msdn page with examples at the bottom:
http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.aspx
Here is my method for doing it.
public void GraphicsToPictureBox (ref PictureBox pb, Graphics graphics,
Int32 width, Int32 height)
{
Bitmap bitmap = new Bitmap(width,height,graphics);
pb.Image = bitmap;
}