Calling my function in webBrowser1_DocumentCompleted does not finish loading the browser - c#

I want to takeScreenShoot() for the first 30 seconds of the video. But when I call it in webBrowser1_DocumentCompleted it stops the video from loading. So the screenshoots will be blank. This is just a rough copy of my code. Is there a way to call takeScreenShoot() after everything is completely loaded and video is playing? or calling it after newForm.ShowDialog(); ?
main(
Form1 newForm = new Form1();
newForm.loadStream();
newForm.ShowDialog();
}
public void loadVideo()
{
//// When the form loads, open this web page.
//this.webBrowser1.Navigate("http://www.dotnetperls.com/");
SuppressScriptErrorsOnly(webBrowser1);
//set browser eumulator to IE8
var appName = Process.GetCurrentProcess().ProcessName + ".exe";
SetIE8KeyforWebBrowserControl(appName);
webBrowser1.Navigate("http://youtu.be/e-ORhEE9VVg?list=PLFgquLnL59alLAsVmUulfe3X-BrPzoYAH");
webBrowser1.ScriptErrorsSuppressed = true;
//Console.WriteLine("loading new new");
}
public void takeScreenShoot()
{
DateTime startTime = DateTime.Now;
int i = 0;
string location = #"F:\Twitch Screenshoot\testing\";
while (true)
{
//If time is greater than 30 seconds start taking picture
if (DateTime.Now.Subtract(startTime).TotalSeconds > 1)
{
if (DateTime.Now.Subtract(startTime).TotalSeconds > i + 1)
{
Console.WriteLine("Taking Picture\n");
// The size of the browser window when we want to take the screenshot (and the size of the resulting bitmap)
Bitmap bitmap = new Bitmap(1024, 768);
Rectangle bitmapRect = new Rectangle(0, 0, 1024, 768);
// This is a method of the WebBrowser control, and the most important part
this.webBrowser1.DrawToBitmap(bitmap, bitmapRect);
// Generate a thumbnail of the screenshot (optional)
System.Drawing.Image origImage = bitmap;
System.Drawing.Image origThumbnail = new Bitmap(120, 90, origImage.PixelFormat);
Graphics oGraphic = Graphics.FromImage(origThumbnail);
oGraphic.CompositingQuality = CompositingQuality.HighQuality;
oGraphic.SmoothingMode = SmoothingMode.HighQuality;
oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
Rectangle oRectangle = new Rectangle(0, 0, 120, 90);
oGraphic.DrawImage(origImage, oRectangle);
// Save the file in PNG format
origThumbnail.Save(location + "Screenshot" + i + ".png", ImageFormat.Png);
origImage.Dispose();
i++;
}
}
//stop taking picture when time is greater than 3.
if (DateTime.Now.Subtract(startTime).TotalMinutes > 1)
{
//should close form here
this.Close();
break;
}
}
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (e.Url.AbsolutePath != (sender as WebBrowser).Url.AbsolutePath)
{
var webBrowser = sender as WebBrowser;
webBrowser.DocumentCompleted -= webBrowser1_DocumentCompleted;
Console.WriteLine("loading {0} \n");
this.takeScreenShoot();
}
}

(Making comment as answer to allow question to close)
I'm pretty sure the issue that the video you linked is Adobe Flash. However taking the screenshot with selenium might work.

Related

Printing a Windows Form fit to an A4 Paper [duplicate]

In C#, I am trying to print an image using PrintDocument class with the below code. The image is of size 1200 px width and 1800 px height. I am trying to print this image in a 4*6 paper using a small zeebra printer. But the program is printing only 4*6 are of the big image. that means it is not adjusting the image to the paper size !
PrintDocument pd = new PrintDocument();
pd.PrintPage += (sender, args) =>
{
Image i = Image.FromFile("C://tesimage.PNG");
Point p = new Point(100, 100);
args.Graphics.DrawImage(i, 10, 10, i.Width, i.Height);
};
pd.Print();
When i print the same image using Window Print (right click and select print, it is scaling automatically to paper size and printing correctly. that means everything came in 4*6 paper.) How do i do the same in my C# program ?
The parameters that you are passing into the DrawImage method should be the size you want the image on the paper rather than the size of the image itself, the DrawImage command will then take care of the scaling for you. Probably the easiest way is to use the following override of the DrawImage command.
args.Graphics.DrawImage(i, args.MarginBounds);
Note: This will skew the image if the proportions of the image are not the same as the rectangle. Some simple math on the size of the image and paper size will allow you to create a new rectangle that fits in the bounds of the paper without skewing the image.
Not to trample on BBoy's already decent answer, but I've done the code that maintains aspect ratio. I took his suggestion, so he should get partial credit here!
PrintDocument pd = new PrintDocument();
pd.DefaultPageSettings.PrinterSettings.PrinterName = "Printer Name";
pd.DefaultPageSettings.Landscape = true; //or false!
pd.PrintPage += (sender, args) =>
{
Image i = Image.FromFile(#"C:\...\...\image.jpg");
Rectangle m = args.MarginBounds;
if ((double)i.Width / (double)i.Height > (double)m.Width / (double)m.Height) // image is wider
{
m.Height = (int)((double)i.Height / (double)i.Width * (double)m.Width);
}
else
{
m.Width = (int)((double)i.Width / (double)i.Height * (double)m.Height);
}
args.Graphics.DrawImage(i, m);
};
pd.Print();
The solution provided by BBoy works fine. But in my case I had to use
e.Graphics.DrawImage(memoryImage, e.PageBounds);
This will print only the form. When I use MarginBounds it prints the entire screen even if the form is smaller than the monitor screen. PageBounds solved that issue. Thanks to BBoy!
You can use my code here
//Print Button Event Handeler
private void btnPrint_Click(object sender, EventArgs e)
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += PrintPage;
//here to select the printer attached to user PC
PrintDialog printDialog1 = new PrintDialog();
printDialog1.Document = pd;
DialogResult result = printDialog1.ShowDialog();
if (result == DialogResult.OK)
{
pd.Print();//this will trigger the Print Event handeler PrintPage
}
}
//The Print Event handeler
private void PrintPage(object o, PrintPageEventArgs e)
{
try
{
if (File.Exists(this.ImagePath))
{
//Load the image from the file
System.Drawing.Image img = System.Drawing.Image.FromFile(#"C:\myimage.jpg");
//Adjust the size of the image to the page to print the full image without loosing any part of it
Rectangle m = e.MarginBounds;
if ((double)img.Width / (double)img.Height > (double)m.Width / (double)m.Height) // image is wider
{
m.Height = (int)((double)img.Height / (double)img.Width * (double)m.Width);
}
else
{
m.Width = (int)((double)img.Width / (double)img.Height * (double)m.Height);
}
e.Graphics.DrawImage(img, m);
}
}
catch (Exception)
{
}
}
Answer:
public void Print(string FileName)
{
StringBuilder logMessage = new StringBuilder();
logMessage.AppendLine(string.Format(CultureInfo.InvariantCulture, "-------------------[ START - {0} - {1} -------------------]", MethodBase.GetCurrentMethod(), DateTime.Now.ToShortDateString()));
logMessage.AppendLine(string.Format(CultureInfo.InvariantCulture, "Parameter: 1: [Name - {0}, Value - {1}", "None]", Convert.ToString("")));
try
{
if (string.IsNullOrWhiteSpace(FileName)) return; // Prevents execution of below statements if filename is not selected.
PrintDocument pd = new PrintDocument();
//Disable the printing document pop-up dialog shown during printing.
PrintController printController = new StandardPrintController();
pd.PrintController = printController;
//For testing only: Hardcoded set paper size to particular paper.
//pd.PrinterSettings.DefaultPageSettings.PaperSize = new PaperSize("Custom 6x4", 720, 478);
//pd.DefaultPageSettings.PaperSize = new PaperSize("Custom 6x4", 720, 478);
pd.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);
pd.PrinterSettings.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);
pd.PrintPage += (sndr, args) =>
{
System.Drawing.Image i = System.Drawing.Image.FromFile(FileName);
//Adjust the size of the image to the page to print the full image without loosing any part of the image.
System.Drawing.Rectangle m = args.MarginBounds;
//Logic below maintains Aspect Ratio.
if ((double)i.Width / (double)i.Height > (double)m.Width / (double)m.Height) // image is wider
{
m.Height = (int)((double)i.Height / (double)i.Width * (double)m.Width);
}
else
{
m.Width = (int)((double)i.Width / (double)i.Height * (double)m.Height);
}
//Calculating optimal orientation.
pd.DefaultPageSettings.Landscape = m.Width > m.Height;
//Putting image in center of page.
m.Y = (int)((((System.Drawing.Printing.PrintDocument)(sndr)).DefaultPageSettings.PaperSize.Height - m.Height) / 2);
m.X = (int)((((System.Drawing.Printing.PrintDocument)(sndr)).DefaultPageSettings.PaperSize.Width - m.Width) / 2);
args.Graphics.DrawImage(i, m);
};
pd.Print();
}
catch (Exception ex)
{
log.ErrorFormat("Error : {0}\n By : {1}-{2}", ex.ToString(), this.GetType(), MethodBase.GetCurrentMethod().Name);
}
finally
{
logMessage.AppendLine(string.Format(CultureInfo.InvariantCulture, "-------------------[ END - {0} - {1} -------------------]", MethodBase.GetCurrentMethod().Name, DateTime.Now.ToShortDateString()));
log.Info(logMessage.ToString());
}
}
Agree with TonyM and BBoy - this is the correct answer for original 4*6 printing of label. (args.PageBounds). This worked for me for printing Endicia API service shipping Labels.
private void SubmitResponseToPrinter(ILabelRequestResponse response)
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += (sender, args) =>
{
Image i = Image.FromFile(response.Labels[0].FullPathFileName.Trim());
args.Graphics.DrawImage(i, args.PageBounds);
};
pd.Print();
}
all these answers has the problem, that's always stretching the image to pagesize and cuts off some content at trying this.
Found a little bit easier way.
My own solution only stretch(is this the right word?) if the image is to large, can use multiply copies and pageorientations.
PrintDialog dlg = new PrintDialog();
if (dlg.ShowDialog() == true)
{
BitmapImage bmi = new BitmapImage(new Uri(strPath));
Image img = new Image();
img.Source = bmi;
if (bmi.PixelWidth < dlg.PrintableAreaWidth ||
bmi.PixelHeight < dlg.PrintableAreaHeight)
{
img.Stretch = Stretch.None;
img.Width = bmi.PixelWidth;
img.Height = bmi.PixelHeight;
}
if (dlg.PrintTicket.PageBorderless == PageBorderless.Borderless)
{
img.Margin = new Thickness(0);
}
else
{
img.Margin = new Thickness(48);
}
img.VerticalAlignment = VerticalAlignment.Top;
img.HorizontalAlignment = HorizontalAlignment.Left;
for (int i = 0; i < dlg.PrintTicket.CopyCount; i++)
{
dlg.PrintVisual(img, "Print a Image");
}
}

Screen recorder crashes when I move the form window

I have followed a bunch of tutorials and made a screen recorder. It works by taking a screenshot and then using the AForge.video addon to convert this to avi format. The programme worked fine, but it ran out of memory after about 20 seconds. This would either crash the program or clear itself with a huge lag spike. To stop this I added a disposal method at the end of every screenshot to clear the memory. This kept the memory usage down however it makes the application hugely unstable. When I move the main window or wait for about 3 minutes on recording, the programme crashes and shows this:
A screenshot of the error
Every time I remove the dispose methods, the programme moves fine but runs out of memory quickly. Maybe I'm just using the dispose method wrong.
Here's the code that crashes the programme. There is way to much code to include it all.
counter = 0;
imagelist = new Bitmap[100000];
Globals.imgcount = 0;
Graphics g;
basePath = sel.ToString();
basePath = #"C:\Users\sim\Videos\Captures";
using (var videowriter = new VideoFileWriter())
{
videowriter.Open(basePath + "timelapse.avi", Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, 9, VideoCodec.MPEG4, 1200000);
while (Globals.recording == true)
{
try
{
try
{
//takes the screenshot
bm = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
//turns it into graphics
g = Graphics.FromImage(bm);
g.CopyFromScreen(0, 0, 0, 0, bm.Size);
counter++;
videowriter.WriteVideoFrame(bm);
//display image
Bitmap original = bm;
Bitmap resized2 = new Bitmap(original, new Size(pictureBox1.Width, pictureBox1.Height));
bm = resized2;
pictureBox1.Image = bm;
Thread.Sleep(10);
if (/*counter % 18 == 0*/ true)
{
try
{
g.Dispose();
bm.Dispose();
original.Dispose();
resized2.Dispose();
}
catch
{
MessageBox.Show("Disposal error");
}
}
}
catch { }
}
catch { }
}
videowriter.Close();
}
I hope that this is enough information to figure something out.
Thanks to anyone who can help.
You are doing many things unnecessary eg creating 3 bitmaps instead of one, setting the image bm to picbox and of cource running the code to main UI. Of cource it will freeze. For an easy and quick fix create a timer. Set the interval to 10. Create a button that will stop the timer and one that will start it and record. In timer instead of calling
repeatedly
pictureBox1.Image = bm;
use
pictureBox1.Invalidate();
Code:
//not good names. change it to something meaningfull
private Bitmap bm;
private Graphics g;
VideoFileWriter videowriter;
private void timer1_Tick( object sender, EventArgs e ) {;
//takes the screenshot
g.CopyFromScreen( 0, 0, 0, 0, bm.Size );
videowriter.WriteVideoFrame(bm);
pictureBox1.Invalidate();
}
The button that starts the recording:
private void Start_Click( object sender, EventArgs e ) {
//create both bitmap and graphics once!
bm = new Bitmap( Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height );
g = Graphics.FromImage( bm );
pictureBox1.Image = bm; //Just once!
basePath = sel.ToString();
basePath = #"C:\Users\sim\Videos\Captures";
videowriter = new VideoFileWriter();
videowriter.Open(basePath + "timelapse.avi", Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, 9, VideoCodec.MPEG4, 1200000);
timer1.Enabled = true;
}
Button to stop recording:
private void Stop_Click( object sender, EventArgs e ) {
timer1.Enabled = false;
pictureBox1.Image = null;
bm.Dispose();
bm = null;
g.Dispose();
g = null;
videowriter.Close();
//I don't know if videowriter can be disposed if so dispose it too and set it to null
}
Also set picturebox SizeMode to StreachImage

Obtain full-page screenshot from WebBrower component

I am attempting to capture a full-page screenshot of any website a user is viewing using the WebBrowser component.
At present, I am able to only able to capture what a user is viewing from within the WebBrowser. However, the screenshot image created is the size of the webpage. For example, below is a (half-sized) screenshot of the BBC website, the black area is actually saved transparent but I've filled it black for visibility.
I have seen solutions where a new WebBrowser instance is used to fetch a fullpage snapshot. However, I need the screenshot to be exactly of the page as the user is viewing it at the time, much like how the full-page screenshot works in Firefox.
My code below that generated the above image:
private void button1_Click(object sender, EventArgs e)
{
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
int scrollWidth = 0;
int scrollHeight = 0;
scrollHeight = webBrowser1.Document.Body.ScrollRectangle.Height;
scrollWidth = webBrowser1.Document.Body.ScrollRectangle.Width;
webBrowser1.Size = new Size(scrollWidth, scrollHeight);
Bitmap bm = new Bitmap(scrollWidth, scrollHeight);
webBrowser1.DrawToBitmap(bm, new Rectangle(0, 0, bm.Width, bm.Height));
bm.Save(#"D:\Screenshots\test.png", ImageFormat.Png);
}
I've got a good working one..
private void button1_Click(object sender, EventArgs e)
{
using (FileDialog fd = new SaveFileDialog())
{
fd.Filter = "Image (*.png)|*.png";
if (fd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
new WebPageSnap(webBrowser1.Url.ToString(), fd.FileName);
//might take 3 or 4 seconds to save cauz it has to load again.
}
}
}
class WebPageSnap
{
WebBrowser wb;
string outFile;
public WebPageSnap(string url, string outputFile)
{
wb = new WebBrowser();
wb.ProgressChanged += wb_ProgressChanged;
outFile = outputFile;
wb.ScriptErrorsSuppressed = true;
wb.ScrollBarsEnabled = false;
wb.Navigate(url);
}
void wb_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
{
if (e.CurrentProgress == e.MaximumProgress)
{
wb.ProgressChanged -= wb_ProgressChanged;
try
{
int scrollWidth = 0;
int scrollHeight = 0;
scrollHeight = wb.Document.Body.ScrollRectangle.Height;
scrollWidth = wb.Document.Body.ScrollRectangle.Width;
wb.Size = new Size(scrollWidth, scrollHeight);
Bitmap bitmap = new Bitmap(wb.Width, wb.Height);
for (int Xcount = 0; Xcount < bitmap.Width; Xcount++)
for (int Ycount = 0; Ycount < bitmap.Height; Ycount++)
bitmap.SetPixel(Xcount, Ycount, Color.Black);
wb.DrawToBitmap(bitmap, new Rectangle(0, 0, wb.Width, wb.Height));
bitmap.Save(outFile, ImageFormat.Png);
}
catch { }
}
}
}
.
;Here's the result
.

print form on the full page instead of a part of it

i want to print my form on the whole page but instead the picture looks like this :
http://i.stack.imgur.com/JSXh2.jpg
it looks very small that's why i need the form to be printed on the whole full page
here is my code :
private void button5_Click(object sender, EventArgs e)
{
CaptureScreen();
printDocument1.Print();
}
private Bitmap _memoryImage;
private void CaptureScreen()
{
// put into using construct because Graphics objects do not
// get automatically disposed when leaving method scope
using (var myGraphics = CreateGraphics())
{
var s = Size;
_memoryImage = new Bitmap(s.Width, s.Height, myGraphics);
using (var memoryGraphics = Graphics.FromImage(_memoryImage))
{
memoryGraphics.CopyFromScreen(Location.X, Location.Y, 0, 0, s);
}
}
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
var wScale = e.MarginBounds.Width / (float)_memoryImage.Width;
var hScale = e.MarginBounds.Height / (float)_memoryImage.Height;
// choose the smaller of the two scales
var scale = wScale < hScale ? wScale : hScale;
// apply scaling to the image
e.Graphics.ScaleTransform(scale, scale);
// print to default printer's page
e.Graphics.DrawImage(_memoryImage, 0, 0);
}
your help would be appreciated
try this to make a bitmap of your form:
Bitmap bm = new Bitmap(f1.Width, f1.Height);
this.DrawToBitmap(bm, new Rectangle(f1.Location, f1.Size));
maybe that's the problem! Report back if it works!
If you want to print it in landscape format than you have to rotate your bitmap using:
public Bitmap rotateInternalFunction(Bitmap bitmap)
{
bitmap.RotateFlip(RotateFlipType.Rotate90FlipNone);
return bitmap;
}

Screenshot of Mainform shows subform. How to make sure subform is closed?

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

Categories