Posts

Showing posts with the label winforms

C# Convert html page to image

* Add references to System.Windows.Forms and System.Drawing public class HtmlRenderer { public static void Render( string inputUrl, string outputPath, Rectangle crop) { WebBrowser wb = new WebBrowser(); wb.ScrollBarsEnabled = false ; wb.ScriptErrorsSuppressed = true ; wb.Navigate(inputUrl); while (wb.ReadyState != WebBrowserReadyState.Complete) { Application.DoEvents(); } wb.Width = wb.Document.Body.ScrollRectangle.Width; wb.Height = wb.Document.Body.ScrollRectangle.Height; using (Bitmap bitmap = new Bitmap(wb.Width, wb.Height)) { wb.DrawToBitmap(bitmap, new Rectangle(0, 0, wb.Width, wb.Height)); wb.Dispose(); Rectangle rect = new Rectangle(crop.Left, crop.Top, wb.Width - crop.Width - crop.Left, wb.Height - crop.Height - crop.Top); Bitmap cropped = bitmap.Clone(rect, bitmap.PixelFormat); cropped.Save(outputPath, ImageFormat.Png); } } } You need to run it from the thread in Si...

Create chart in C# Windows Forms and save it to file

Image
static class Program { [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault( false ); using (var chartForm = new ChartForm()) { FillData(chartForm.Chart); chartForm.Show(); //save chartForm.Chart.SaveImage(@" Chart.png ", ChartImageFormat.Png); chartForm.Close(); } } public static void FillData(Chart chart) { chart.Titles[0].Text = " Price/Volume Chart "; chart.Series[0].Name = " Price "; chart.Series[1].Name = " Volume "; var random = new Random(); for ( int i = 0; i < 10; i++) { var date = DateTime.Today.AddDays(i); chart.Series[0].Points.Add( new DataPoint(date.ToOADate(), random.NextDouble() * 1e3)); chart.Series[1].Points.Add( new DataPoint(date.ToOADate(), ran...

Open Windows Form from nunit test

[Test] public void Test() { var form = new Form1(); Application.Run(form); }

Disable C# Form resizing

form.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;

C# Updating GUI from different thread

void MyEventHandler( object sender, EventArgs e) { if (this.InvokeRequired) { this.Invoke( new EventHandler(MyEventHandler), new object [] { sender, e } ); } else { //update GUI here } } void MyEventHandler( object sender, MyEventArgs e) { if (this.InvokeRequired) { this.BeginInvoke( new EventHandler<MyEventArgs>(MyEventHandler), new object [] { sender, e }); } else { //update GUI here } }

Events in C#

public class Messenger { public delegate void MessageEventHandler( object source, MessageEvent e); public event MessageEventHandler changed; public void Run() { fireMessageEvent("Message"); } private void fireMessageEvent(string text) { log.Info(text); if (changed != null ) { changed(this, new MessageEvent(text)); } } } public class MessageEvent : EventArgs { private readonly string message; public MessageEvent(string message) { this.message = message; } public string Message { get { return this.message; } } } public class Client { Messenger messenger = new Messenger(); public Client(){ messenger.changed += new Messenger.MessengerEventHandler(messenger_changed); } void messenger_changed( object source, MessageEvent e) { Console.WriteLine(e.Message); } }

Autoscroll in C# RichTextBox

using System.Runtime.InteropServices; #region WinAPI //WinAPI-Declaration for SendMessage [DllImport(" user32.dll ")] public static extern IntPtr SendMessage( IntPtr window, int message, int wparam, int lparam); const int WM_VSCROLL = 0x115; const int SB_BOTTOM = 7; #endregion SendMessage(richTextBox1.Handle, WM_VSCROLL, SB_BOTTOM, 0);