Screenshot program over loads RAM [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
So i'm relatively new to C# and i want to ride an application that shows you the color of the pixel you hover over but the with this code there seems to be a big problem.
thx a lot for the help
the code:
private void timer1_Tick(object sender, EventArgs e)
{
b = null;
b = Screenshot();
Color color = b.GetPixel(Cursor.Position.X, Cursor.Position.Y);
label1.Text = color.Name;
label2.Text = Cursor.Position.Y.ToString() + Cursor.Position.X.ToString();
}
private Bitmap Screenshot()
{
Bitmap Screen = new Bitmap(SystemInformation.VirtualScreen.Width,SystemInformation.VirtualScreen.Height);
Graphics g = Graphics.FromImage(Screen);
g.CopyFromScreen(SystemInformation.VirtualScreen.X,SystemInformation.VirtualScreen.Y, 0, 0, Screen.Size);
return Screen;
}

You neither need a timer nor copy the whole screen. Just add a MouseMove handler like this:
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
using (var bitmap = new Bitmap(1, 1))
{
var graphics = Graphics.FromImage(bitmap);
var position = PointToScreen(e.Location);
graphics.CopyFromScreen(position.X, position.Y, 0, 0, new Size(1, 1));
var color = bitmap.GetPixel(0, 0);
label1.Text = color.ToString();
}
}
Or reuse the Bitmap:
private readonly Bitmap bitmap = new Bitmap(1, 1);
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
var graphics = Graphics.FromImage(bitmap);
var position = PointToScreen(e.Location);
graphics.CopyFromScreen(position.X, position.Y, 0, 0, new Size(1, 1));
var color = bitmap.GetPixel(0, 0);
label1.Text = color.ToString();
}

Your code works perfectly when you use it in a correct way: (no global 'b')
private void Timer1_Tick(object sender, EventArgs e)
{
using (var b = Screenshot())
{
label1.Text = b.GetPixel(Cursor.Position.X, Cursor.Position.Y).Name;
label2.Text = Cursor.Position.ToString();
}
}
private static Bitmap Screenshot()
{
var Screen = new Bitmap(SystemInformation.VirtualScreen.Width, SystemInformation.VirtualScreen.Height);
var g = Graphics.FromImage(Screen);
g.CopyFromScreen(SystemInformation.VirtualScreen.X, SystemInformation.VirtualScreen.Y, 0, 0, Screen.Size);
return Screen;
}
But of course, try to copy whole screen only for a single pixel is wrong....
Therefore use something like the fixed code from Clemens.
e.g.:
private readonly Bitmap screen = new Bitmap(1, 1);
private static readonly Size size = new Size(1, 1);
private void Timer1_Tick(object sender, EventArgs e)
{
using (var g = Graphics.FromImage(screen))
{
g.CopyFromScreen(Cursor.Position.X, Cursor.Position.Y, 0, 0, size);
label1.Text = screen.GetPixel(0, 0).Name;
label2.Text = Cursor.Position.ToString();
}
}
And do not forget to dispose Bitmap in the form Dispose method.

Related

How can i remove the button of my form when i go to the print preview

Hello guys goodevening to you... how can i remove the button 6 to my print preview ? i dont have any idea even when i false the visible of the button6
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);
}
private void button6_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();
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawImage(bitmap, 0, 0);
}
}
}
//CODE
private void button6_Click(object sender, EventArgs e)
{
button6.Visible = false;//add here
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();
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawImage(bitmap, 0, 0);
}
private void printPreviewDialog1_FormClosed(object sender, FormClosedEventArgs e)
{
button6.Visible = true;//this is to enable button after closing print screen
}
In order for the button to actually become invisible, you have to force the events to actually run.
In your button6_Click event handler you begin with the lines:
button6.Visible = false;
Application.DoEvents();
The DoEvents() method forces the events in the event queue to run. This is because you run on the UI thread. I would not recomend you move the copying operation to another thread, but another way would be to start an UI timer (found under Components in the tools menu) and have that timer timout at a few ms. You can then have the copying operation there. But the easiest solution is to simply add the Application.DoEvents() so that is my recommendation.

C# paint program won't create any lines on the screen since I tried to draw using a bitmap

Here is my source code. I can't seem to get the bitmap to show the lines drawn on the panel when I move the mouse with the button pressed. Frustrated and looking for someone to help me finish the code so I can complete the app for my 9-yo daughter. Thank you in advance...
namespace TV_PAINT
{
public partial class ALANA_PAINT : Form
{
Graphics g;
Pen p = new Pen(Color.Black, 7);
Point sp = new Point(0, 0);
Point ep = new Point(0, 0);
int m = 0;
Bitmap BP;
public ALANA_PAINT()
{
InitializeComponent();
tb1.Text = p.Width.ToString();
BP = new Bitmap(pnl1.ClientSize.Width, pnl1.ClientSize.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
}
private void closeButton_Click(object sender, EventArgs e)
{
pnl1.Dispose();
p.Dispose();
this.Close();
}
private void clearButton_Click(object sender, EventArgs e)
{
//pnl1.Invalidate();
p.Color = System.Drawing.Color.Black;
p.Width = 7;
tb1.Text = p.Width.ToString();
//pnl1.Invalidate();
}
private void pnl1_MouseDown(object sender, MouseEventArgs e)
{
sp = e.Location;
if (e.Button == MouseButtons.Left)
m = 1;
if (e.Button == MouseButtons.Right)
m = 1;
}
private void pnl1_MouseMove(object sender, MouseEventArgs e)
{
if (m == 1)
{
ep = e.Location;
//g = pnl1.CreateGraphics();
Graphics g = Graphics.FromImage(BP);
g.DrawLine(p, sp, ep);
}
sp = ep;
}
private void pnl1_MouseUp(object sender, MouseEventArgs e)
{
m = 0;
}
BP is just a variable in the form. As I can see, it is not displayed anywhere in your form. Why do you need a bitmap for it.
You can do something like this, just get the graphics of your form, and draw using that graphic. https://msdn.microsoft.com/en-us/library/ztxk24yx(v=vs.110).aspx
Noted: you need to do it on PaintEvent of the form, otherwise your drawing will be removed after the next repaint, so you need some variables to store all of your lines, then draw all of them in the paint event.
System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red);
System.Drawing.Graphics formGraphics;
formGraphics = this.CreateGraphics();
formGraphics.FillRectangle(myBrush, new Rectangle(0, 0, 200, 300));
myBrush.Dispose();
formGraphics.Dispose();
Updated:
If you want to save your change to a bitmap. You can use Form.DrawToBitmap to save your drawing in the form to a bitmap, then call bitmap.Save() to a file in directory.

C# Windows Form printing - prints wrong area of screen [duplicate]

This question already has answers here:
How to configure an app to run correctly on a machine with a high DPI setting (e.g. 150%)?
(6 answers)
Closed 6 years ago.
I'm trying to print my Windows Form. My solution is:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawImage(bmp, 0, 0);
}
Bitmap bmp;
private void button1_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
bmp = new Bitmap(this.Size.Width, this.Size.Height, g);
Graphics mg = Graphics.FromImage(bmp);
mg.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, this.Size);
printDialog1.ShowDialog();
printDocument1.Print();
printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
}
}
But it gives me this result:
(frame is made by me to show the borders of the printed paper)
How can i solve this problem?
try this
private void button1_Click(object sender, EventArgs e)
{
//Add a Panel control.
Panel panel = new Panel();
this.Controls.Add(panel);
//Create a Bitmap of size same as that of the Form.
Graphics grp = panel.CreateGraphics();
Size formSize = this.ClientSize;
bitmap = new Bitmap(formSize.Width, formSize.Height, grp);
grp = Graphics.FromImage(bitmap);
//Copy screen area that that the Panel covers.
Point panelLocation = PointToScreen(panel.Location);
grp.CopyFromScreen(panelLocation.X, panelLocation.Y, 0, 0, formSize);
//Show the Print Preview Dialog.
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.PrintPreviewControl.Zoom = 1;
printPreviewDialog1.ShowDialog();
}
Ref : http://www.aspsnippets.com/Articles/Print-contents-of-Form-in-Windows-Forms-WinForms-Application-using-C-and-VBNet.aspx

Compare two images with PictureBox controls

I'm trying to achieve an effect similar to this site. I am basically trying to implement a way to "compare" two similar images (with different colors, etc). I managed to do this in a not so brilliant way using two PictureBox controls (Winforms) one next to the other, and changing their Size and Location attributes on a MouseMove event.
The result works, but it flickers a lot and it's not really the best way to do it.
Is there a better way to do this, maybe with a WPF or by changing the code in any way? Here it is:
private void pbImg1_MouseMove(object sender, MouseEventArgs e)
{
pbImg2.Image = CropImage(array[1], new Rectangle(pbImg1.Size.Width, 0, totalSize.Width - pbImg1.Size.Width, 240));
pbImg1.Size = new Size(e.X, pbImg1.Height);
pbImg2.Location = new Point(pbImg1.Size.Width + pbImg1.Location.X, pbImg2.Location.Y);
pbImg2.Size = new Size(totalSize.Width - pbImg1.Size.Width, 240);
lpbImg1Size.Text = pbImg1.Size.ToString();
lpbImg2Size.Text = pbImg2.Size.ToString();
lpbImg1Location.Text = pbImg1.Location.ToString();
lpbImg2Location.Text = pbImg2.Location.ToString();
}
private void pbImg2_MouseMove(object sender, MouseEventArgs e)
{
pbImg1.Image = CropImage(array[0], new Rectangle(0, 0, totalSize.Width - pbImg2.Size.Width, 240));
pbImg1.Size = new Size(pbImg1.Width + e.X, 240);
lpbImg1Size.Text = pbImg1.Size.ToString();
lpbImg2Size.Text = pbImg2.Size.ToString();
lpbImg1Location.Text = pbImg1.Location.ToString();
lpbImg2Location.Text = pbImg2.Location.ToString();
}
public Bitmap CropImage(Bitmap source, Rectangle section)
{
// An empty bitmap which will hold the cropped image
//TRY CATCH
Bitmap bmp = new Bitmap(section.Width, section.Height);
Graphics g = Graphics.FromImage(bmp);
// Draw the given area (section) of the source image
// at location 0,0 on the empty bitmap (bmp)
g.DrawImage(source, 0, 0, section, GraphicsUnit.Pixel);
return bmp;
}
And here you can see the behavior of the program:
https://gfycat.com/VillainousReadyAmazonparrot
You need two images imageLeft, imageRight with the same size as the picturebox:
private int pos = 0; //x coordinate of mouse in picturebox
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if(pos == 0)
{
e.Graphics.DrawImage(Properties.Resources.imageRight, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height));
}
else
{
e.Graphics.DrawImage(Properties.Resources.imageLeft, new Rectangle(0, 0, pos, pictureBox1.Height),
new Rectangle(0, 0, pos, pictureBox1.Height), GraphicsUnit.Pixel);
e.Graphics.DrawImage(Properties.Resources.imageRight, new Rectangle(pos, 0, pictureBox1.Width - pos, pictureBox1.Height),
new Rectangle(pos, 0, pictureBox1.Width - pos, pictureBox1.Height), GraphicsUnit.Pixel);
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
pos = e.X;
pictureBox1.Invalidate();
}

Printing form gives me blank page

Using the code example from MSDN on how to print a windows form, I have the altered mine to bring up the printer options first and then print, but I keep receiving a blank page. Using CopyFromScreen, I am giving the coordinates of the forms source X & Y, but for the destination I have tried 0 as well as this.Location.X & Y. Is there another way to capture the image?
private void printButton_Click(object sender, EventArgs e)
{
CaptureScreen();
printDialog1.AllowSomePages = true;
printDialog1.ShowHelp = true;
printDialog1.Document = printDoc1;
DialogResult result = printDialog1.ShowDialog();
if (result == DialogResult.OK)
{
printDoc1.Print();
}
}
private void CaptureScreen()
{
Graphics myGraphics = this.CreateGraphics();
Size s = this.Size;
memoryImage = new Bitmap(s.Width, s.Height, myGraphics);
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
memoryGraphics.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, s);
}
void printDoc1_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawImage(memoryImage, 0, 0);
}

Categories

Resources