Rendering form to bitmap - winforms

I would like to render Form to bitmap... Is it possible in .net 2.0?

Sure, you can call Control.DrawToBitmap() to render a control to a bitmap. For more general drawing, you can create a Bitmap and then use Graphics.FromImage() to create a Graphics instance. You can then draw to this graphics instance as normal.
Here's a simple form that can draw itself (just add a button and double click it to add the Click event handler:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Bitmap b = new Bitmap(Width, Height);
DrawToBitmap(b, new Rectangle(0, 0, Width, Height));
b.Save("Test.bmp");
}
}

Related

using delay in tab control before the data is insert into textbox in wpf

I am new in wpf. I am using the tabcontrol. and there are two tabs. I want that on change the tab all the contents of tab load. and then I am inserting some text in a text box. I want delay of 5 sec before text is insert in textbox. I illustrate this with images.
below is the open tabcontrol.
When I click on connect its display the following.
on right side there is a textbox with text "vokkey, Dave". I want that after the tab load it's wait for 5 second and then the text "vokkey,dave" appear in textbox. on which event should i work.? and for delay what should i do?
It is customary to use a DispatcherTimer for these situations... put this into your UserControl:
In the constructor:
Loaded += YourControl_Loaded;
Then in the code behind of the UserControl:
private void YourControl_Loaded(object sender, RoutedEventArgs e)
{
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = new TimeSpan(0, 0, 5);
timer.Tick += Timer_Tick;
timer.Start();
}
...
private void Timer_Tick(object sender, EventArgs e)
{
TextBox.Text = "vokkey, Dave";
timer.Stop();
}
You can find out more about the DispatcherTimer from the DispatcherTimer Class page at MSDN.

Use wait icon when a window is loaded in wpf

I am writing a wpf application which pulls data from excel sheet on clicking a button and loads another window where a datagrid is present which displays the result.
Now it takes 10-12 seconds to load the second window and during this time my application freezes. Now what I want is to display a little circular ribbon shaped button which will revolve and a "Please wait" text is displayed. This will be displayed in the centre of the first window and the other contents of the first window will become dimmer.
After the second windows is loaded , the first window closes.
Please tell me how to do this.
The problem is resolved. Thanks a lot for your help. Following is the code I used.
namespace ScoreX
{
public partial class Score : Window
{
Applications ap;
public Score()
{
InitializeComponent();
}
private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
//cb is Circular progress bar
cbProgress.Visibility = Visibility.Hidden;
//Some codes
}
private void btnProceed_Click(object sender, RoutedEventArgs e)
{
//Some lines of Codes
Thread t1 = new Thread(new ThreadStart(CalculateData));
t1.SetApartmentState(ApartmentState.STA);
t1.Start();
cbProgress.Visibility = Visibility.Visible;
}
private void CalculateData()
{
//Some codes
Dispatcher.Invoke(DispatcherPriority.Normal, (Action)delegate()
{
ap = new Applications();
this.Close();
ap.ShowDialog();
}
);
}

Second Window Positioning WPF

I have MainWindow which has a button that allows it to open another WPF Window. I want this window to open always on the right hand side of the MainWindow practically right next to it.
How can I do this? This needs to work even if the width of the MainWindow changes as I have various buttons on the MainWindow that can change the size of the MainWindow depending on what panel is visible.
You can calculate where you want the new window if you have a reference to the other window.
Get the other windows position by accessing the Left and Top properties and its width by accessing the ActualWidth or the Width property.
Now you can calculate the new windows position by adding Left + Width + Some spacing.
Check out the documentation for the Left property here:
http://msdn.microsoft.com/en-us/library/system.windows.window.left.aspx
The others behave similarily.
You need to set manual startup location for second window in properties or in code:
WindowStartupLocation = WindowStartupLocation.Manual;
On events Loaded, SizeChanged, LocationChanged of first window, you should adjust position of second window like this:
public void AdjustPosition()
{
window2.Left = Application.Current.MainWindow.Left + Application.Current.MainWindow.ActualWidth;
window2.Top = Application.Current.MainWindow.Top;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
AdjustPosition();
}
private void MainWindow_SizeChanged(object sender, SizeChangedEventArgs e)
{
AdjustPosition();
}
void MainWindow_LocationChanged(object sender, EventArgs e)
{
AdjustPosition();
}

How do I get an image from a file in a silverlight C# library into a writeable bitmap without using ImageLoaded event?

I need to have an image in my silverlight library and load it into a bitmap. I want to just refer to it like a resource, but not sure how to go about it. I don't have any xaml at all in this library, but what I read seems to indicate I need to do it with xaml.
Here is how I did it in a sample solution using the imageLoaded event. (you know how silverlight just loves async stuff!) The image properties is set to resource/copy always.
public partial class MainPage : UserControl
{
WriteableBitmap myIcon = new WriteableBitmap(100, 100);
public MainPage()
{
InitializeComponent();
LoadImages();
}
public void LoadImages()
{
BitmapImage bmi = new BitmapImage();
bmi.ImageOpened += ImagesLoaded;
bmi.CreateOptions = BitmapCreateOptions.None;
bmi.UriSource = new Uri(App.Current.Host.Source, "/ClientBin/HouseLogo.png");
}
public void ImagesLoaded(object sender, RoutedEventArgs e)
{
BitmapImage bm = (BitmapImage)sender;
myIcon = new WriteableBitmap(bm);
}
private void btnPdf_Click(object sender, RoutedEventArgs e)
{
PDFdoc doc = new PDFdoc(32.0, 32.0, myIcon );
}
}
First of all you say this is a silverlight library hence "Content" images are not useful, you will need to specify the "Resource" build action on your images in this library project. Hence the Url you need to access the image resource is something like "/YourLibraryNameDllName;component/Images/HouseLogo.png". Where you have a folder in your project called "Images" where you are placing these pngs you want to load from your dll.
With that in place you can load the png into a WriteableBitmap without the async pattern using this chunk of code.
StreamResourceInfo sri = Application.GetResourceStream(new Uri("/YourLibraryNameDllName;component/Images/HouseLogo.png", UriKind.Relative));
BitmapSource source = new BitmapImage();
source.SetSource(sri.Stream);
WriteableBitmap myIcon = new WriteableBitmap(source);

WPF - removing a control that has a DispatcherTimer doesn't seem to go away

I've seen the question asked on StackOverflow on how to properly remove controls in WPF. Generally there is some comment how you don't dispose them manually(or can't) and as long as you are not holding a reference to them they will be cleared eventually by the GC. I noticed quite by accident that one of my controls that I thought I removed was sticking around and still doing work even though I removed it from its parent.
I've recreated the example in as few lines as possible. The control has a DispatcherTimer.
Here is the WPF code behind for the control I want to remove.
public partial class MyControl : UserControl
{
private DispatcherTimer timer;
public MyControl()
{
InitializeComponent();
timer = new DispatcherTimer();
timer.Tick += TimerOnTick;
timer.Interval = TimeSpan.FromSeconds(1);
timer.Start();
}
private void TimerOnTick(object sender, EventArgs args)
{
//this continues to get written out even after this control is removed.
System.Diagnostics.Debug.WriteLine("Tick From MyControl.");
}
}
Here is the code behind for a window that adds and removes my control.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void AddClicked(object sender, RoutedEventArgs e)
{
anyControlHolder.Children.Add(new MyControl());
}
private void RemoveClicked(object sender, RoutedEventArgs e)
{
anyControlHolder.Children.Clear();
}
}
The problem I'm having when I run this code and then dynamically add and remove the custom usercontrol (MyControl) is that it's timer keeps ticking (in this example you can see it write out a message in the output window) and it keeps doing work in its tick event.
What pattern should I use to at least cause the timer to stop ticking when the control is removed?
You can hook into your control's Unloaded event and call timer.Stop() inside. I just tested this scenario, and the Unloaded event is raised when anyControlHolder.Children.Clear() was called, thus stopping the debug messages.
Code:
public partial class MyControl : UserControl {
private DispatcherTimer timer;
public MyControl() {
InitializeComponent();
this.Loaded += new RoutedEventHandler(MyControl_Loaded);
this.Unloaded += new RoutedEventHandler(MyControl_Unloaded);
}
void MyControl_Loaded(object sender, RoutedEventArgs e) {
timer = new DispatcherTimer();
timer.Tick += TimerOnTick;
timer.Interval = TimeSpan.FromSeconds(1);
timer.Start();
}
void MyControl_Unloaded(object sender, RoutedEventArgs e) {
timer.Stop();
}
private void TimerOnTick(object sender, EventArgs args) {
Debug.WriteLine("Tick From MyControl.");
}
}
Hope this helps!
The other thing I would would be to remove the event handler that you added for your tick event. Any thing that you manually add you should remove.
As far as implementing a WeakEventPattern you may want to look at this article

Resources