CopyFromScreen Copy not correct location only on Touchdisplays - c#

In my c# i use CopyFromScreen
On many pc's the script ar always ok,
the panel1 in my Form will be saved as image
but when i use touchdisplays in the image is evrything but newer the panel1
i can not understand why?
private void buttonInsert_Click(object sender, EventArgs e)
{
Image bmp = new Bitmap(panel1.Width, panel1.Height);
var gg = Graphics.FromImage(bmp);
var rect = panel1.RectangleToScreen(panel1.ClientRectangle);
gg.CopyFromScreen(rect.Location, Point.Empty, panel1.Size);
bmp.Save(#"C:\Temp\test.jpg", ImageFormat.Jpeg);
Graphics g = panel1.CreateGraphics();
g.Clear(Color.WhiteSmoke);
}
thx for your help

I found it out, the problem was the windows scaling. When i set it on 100% the Problem solved.
thx a lot

Related

Draw 2 pictureboxes in one bitmap

I'm a c# beginner and I'm wondering if there is a way that I could draw 2 images from 2 different pictureboxes and save it as a one image using the method DrawToBitmap.
I can preview everything nice in the program itself (it looks good), but the main problem is when I save the picture, It's just showing the picturebox1 with a blank-alike icon of the picturebox2 in the middle :/
Here's the part of my code and it's not working as it should
pictureBox2.ImageLocation = potDoSlike;
Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
pictureBox1.DrawToBitmap(bmp, pictureBox1.Bounds);
pictureBox2.DrawToBitmap(bmp, pictureBox2.Bounds);
bmp.Save(#"D:\asd.jpg");
I figured it out! The problem was that I didn't save anything to the pictureBox.Image and it couldn't draw nothing out... well here's the code!
pictureBox2.Parent = pictureBox1;
pictureBox2.ImageLocation = potDoSlike;
pictureBox2.Image = Image.FromFile(potDoSlike);
Bitmap bmp = new Bitmap(pictureBox1.Image);
pictureBox1.DrawToBitmap(bmp, pictureBox1.Bounds);
pictureBox2.DrawToBitmap(bmp, pictureBox2.Bounds);
bmp.Save(#"D:\asd.jpg");

Why does this code save the screenshot as bmp not gif, and why doesn't it save the whole area?

I have a timer tick event set to 250ms:
private void timer4_Tick(object sender, EventArgs e)
{
if (pictureboximagestosavecount == 72)
{
timer4.Enabled = false;
}
else
{
Bitmap bmp = new Bitmap(this.Width, this.Height);
Rectangle rect = new Rectangle(this.Location.X, this.Location.Y, this.Width, this.Height);
pictureboximagestosavecount++;
savePictureBox(pictureBox1, #"c:\temp\pboximages\" + pictureboximagestosavecount.ToString("D6") + "pbimg.gif");
this.DrawToBitmap(bmp, rect);
bmp.Save(#"c:\temp\pboximages\" + pictureboximagestosavecount.ToString("D6") + "form.gif");
}
}
First I'm saving the pictureBox as gif using the method savePictureBox.
Second I save the form1:
bmp.Save(#"c:\temp\pboximages\" + pictureboximagestosavecount.ToString("D6") + "form.gif");
After timer4 is stopped I have abutton click event where I create animated gif from the saved files:
private void animatedgifbutton_Click(object sender, EventArgs e)
{
DirectoryInfo di1;
FileInfo[] fi1;
di1 = new DirectoryInfo(#"c:\temp\pboximages\");
fi1 = di1.GetFiles("*form.gif");
List<string> newImages = new List<string>();
for (int i = 0; i < fi1.Length; i++)
{
newImages.Add(fi1[i].FullName);
}
animatedgif.MakeGIF(newImages, #"c:\temp\pboximages\animated1.gif", 6, true);
}
When I'm doing *.form.gif and I see in the List newImages only the form gifs files the animatedgif.MakeGIF throw error since it need to get List of gifs files but I guess that when I save the form it's saving it as bitmap and not real gif.
How can I take a screenshot of form1 and save it as real gif ?
When I save the pictureBox1 to the hard disk it is GIF and there are no problems.
The problem is with saving the form.
EDIT:
I also saw now that the way I'm saving a screenshot of the form1 is not good it's not saving the whole form area only part of it. I gues using this bmp and rect is not good.
This is what I get for example when I'm saving a screenshot of the form:
In other words I need in timer4 tick event to save the form screen the whole form to the hard disk as a gif file real gif file not bmp.
It doesn't save as a proper GIF because you don't tell it to. The extension is not enough, you need to add the ImageFormat to the Save call!
And it doesn't save the whole area of the Form because you don't use the right Rectangle to control it.
Both issues go away very simply if you use the modified savePictueBox function:
void saveControl(Control Ctl, string fileName)
{
Rectangle Rect = Ctl.ClientRectangle;
// if (Ctl is Form) Rect = Ctl.Bounds; // (*)
using (Bitmap bmp = new Bitmap(Rect.Width, Rect.Height))
{
Ctl.DrawToBitmap(bmp, Rect );
bmp.Save(fileName, System.Drawing.Imaging.ImageFormat.Gif);
}
}
Note that this uses the ClientRectangle, that is, it leaves out the Form border. If you want if to include the Border you can uncomment the line (*)
Now you can call it as
saveControl(pictureBox1, yourFileName);
and as
saveControl(this, yourOtherFileName);
Two notes on Animated Gifs:
Since you are recording the Gifs in real-time don't try to create the animation at the same time!
I found this post interesting. fireydude's answer does work, once you have included all those references from the WPF world..Not sure how to get the best quality and unfortunately there is no control over the timing, so far..

CopyFromScreen not working

This one is slightly confusing...
I am using Adobe's PDF Viewer control to view PDFs but I want the user to be able to drag an image onto the PDF and then when they click save it adds the image to the PDF at that location.
Implementing the PDF viewer proved quite difficult but I decided in the end to use Adobe's control, take a picture and then allow the user to draw the image ontop of the picture of the PDF. When they click save I am going to use PDFSharp to put the image onto the PDF once I've worked out where it goes but the problem I have at the moment is that I can't get a picture of the PDF.
The following code is used to get the picture but the Panel that it is attached to just appears with a white background with a red 'X' and border...
using (Bitmap bitmap = new Bitmap(adobePDFViewer1.Width, adobePDFViewer1.Height))
{
using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(new Point(adobePDFViewer1.Left, adobePDFViewer1.Top), Point.Empty, adobePDFViewer1.Size);
}
panelOverPdfViewer.BackgroundImage = bitmap;
}
I don't think this is the best way of doing it but I couldn't work out any others. Any help would be appreciated!
EDIT:
Following a very helpful answer below this is working code:
Here is the code I used:
Bitmap printscreen = new Bitmap(adobePDFViewer1.Width, adobePDFViewer1.Height);
Graphics graphics = Graphics.FromImage(printscreen as Image);
int left = this.Left + 396;
int top = this.Top + 30;
graphics.CopyFromScreen(left, top, 0, 0, printscreen.Size);
pictureBoxOverPDFView.Image = printscreen;
Look at this this Print-Screen
and try this for test work of CopyFromScreen
private void PrintScreen()
{
Bitmap printscreen = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics graphics = Graphics.FromImage(printscreen as Image);
graphics.CopyFromScreen(0, 0, 0, 0, printscreen.Size);
printscreen.Save(#"C:\Temp\printscreen.jpg", ImageFormat.Jpeg);
}

How to get an image of what System.Drawing.Graphics object draws?

See the following method:
void Paint(System.Drawing.Graphics g)
{
//How can I start record what 'g' will draw to an image object?
g.DrawLine(0,0,50,50);
g.DrawImage(...);
..
..
etc.
}
Now how can I get An Image about what 'g' drew?
Thanks :)
You could try...
using (Graphics g=Graphics.FromImage(inImage))
{
g.Clear(Color.White);
g.DrawLine(0,0,50,50);
}
This will then draw the line on to the image. Just make sure the image is big enough...
Also you can draw straight on to a form by overriding the OnPaint event and getting the graphics object from the eventArgs.
You can do this one
Bitmap bmp;
...
{
InitializeComponent();
bmp = new Bitmap(this.Width,this.Height,Graphics.FromHwnd(this.Handle));
}
void Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = Graphics.FromImage(bmp);
g.DrawLine(0,0,50,50);
..
..
e.Graphics.DrawImage(bmp,0,0);
}

Display images with PictureBox in c#

I have write below codes in c# to display an image in PictureBox but when run the application, nothing shown...
Please help me to fix that.
here is my code:
private void button1_Click(object sender, EventArgs e)
{
PictureBox p =new PictureBox();
p.ImageLocation = "1.jpg"
p.Location = new Point(100, 75);
}
Add this line:
this.Controls.Add(p);
PictureBox.Image = new Bitmap("yourImage.jpg");
The formats supported
are: BMP, EMF, EXIF, GIF, ICON, JPEG, PNG, TIFF and WMF.
It's possible that the PictureBox size is small and your image is too big (check the SizeMode property to "StrechImage")
I was using a high res PNG icon with transparent background and took a little time to figure out.

Categories