DrawToBitmap on Panel is blank - c#

So I've written a class that has a stores some test results info and then a control that displays that info to the user. I want to put a print function on this class to draw the control at a full page size and print it. However it always comes out blank. The code see the panel as a control because it could be some other type of value. I figure there must be something simple I'm missing.
void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
System.Drawing.Size oldSize = printData.Size;
printData.Size = new System.Drawing.Size(e.MarginBounds.Width, e.MarginBounds.Height);
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(printData.Size.Width, printData.Size.Height);
InvertZOrderOfControls(printData.Controls);
printData.DrawToBitmap(bitmap, new System.Drawing.Rectangle(0, 0, printData.Size.Width, printData.Size.Height));
InvertZOrderOfControls(printData.Controls);
e.Graphics.DrawImage(bitmap, e.MarginBounds.Location);
bitmap.Save(#"C:\Users\jdudley\Documents\File.bmp");
printData.Size = oldSize;
}
Following this advice of this thread inverted the Z-Order of the controls but it didn't change anything.
The save call was added for debugging. It looks like it's actually rendering the background color of the panel without any of the controls.
Edit: This is in the context of printing but I have not issue with printing what so ever. My error is in creating the bitmap. The save line I added proves this because it creates a blank bitmap file.

Change your entire event to this
void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(printData.Width, printData.Height);
printData.DrawToBitmap(bitmap, new System.Drawing.Rectangle(new Point(0, 0), printData.Size));
e.Graphics.DrawImage(bitmap, e.MarginBounds.Location);
}
Edit
This is my whole Project. I created a panel named printData and I added two buttons, I attached an event to button1.
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
PrintDocument printDocument = new PrintDocument();
public Form1()
{
InitializeComponent();
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
}
void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(printData.Width, printData.Height);
printData.DrawToBitmap(bitmap, new System.Drawing.Rectangle(new Point(0, 0), printData.Size));
e.Graphics.DrawImage(bitmap, e.MarginBounds.Location);
}
private void button1_Click(object sender, EventArgs e)
{
pd.Print();
}
}
}
You have to try this and see if it works, or else I wont be able to sleep tonight!!

Related

Relocate form created inside a previous form

I have two form classes (IHM and HelpPanel). In my first loaded form (IHM) I call the function bHclass_Click to create and load a HelpPanel. The PopulatePCBclasses() function basically creates all the controls that will be displayed.
My issue is that this HelpPanel is always displayed at the exact same location no matter what value is input in it's Location property.
I tried also by setting the Top and Left attributes but result was the same.
Do I miss an instruction?
private void bHclass_Click(object sender, EventArgs e)
{
HelpPanel hp = new HelpPanel();
hp.PopulatePCBclasses();
hp.Location = new Point(10, 500);
hp.Show();
}
Try setting the start position as manual before changing the location:
private void bHclass_Click(object sender, EventArgs e)
{
HelpPanel hp = new HelpPanel();
hp.PopulatePCBclasses();
hp.StartPosition = FormStartPosition.Manual;
hp.Location = new Point(10, 500);
hp.Show();
}

Deleting graphics

I am trying to delete all the graphics on a group box when pressing a button, but I cannot figure it out how. Adding a new background to the group box seems slow, I want to make it faster.
Moreover, I've read about the Invalidate() method, but it doesn't do what I need.
The piece of code I am talking about :
private void button1_Click(object sender, EventArgs e)
{
groupBox0.Invalidate();
}
private void groupBox0_Paint(object sender, PaintEventArgs e)
{
Graphics graphObj = groupBox0.CreateGraphics();
Pen mypen = new Pen(System.Drawing.Color.Red, 2);
graphObj.DrawRectangle(mypen, 0, 0, 10, 10);
}
When I run the code it does nothing, the graphics are still there. What can I do or change ?

C# WinForm redirect draw to bitmap, nothing to screen

I'm wondering if there is any way to disable screen-drawing for a winform and only draw to a bitmap. What I'm actually trying to achieve is to create a "live image" based on a form but without having to actually having the form visible.
I tried DrawToBitmap while the form was minimized, but this was highly unstable, didn't work and finally crashed.
Ok so I ended up solving this in a bit different way. Following code gets you a Live Messenger-like taskbar thumbnail by drawing you hidden UserControl to a Bitmap and using that as the thumbnail.
Holding your mouse over the taskbar icon still gets you some small thing in the upper left corner. Doesn't bother me but please tell me if you know how to get rid of it!
Make sure you have the Windows API Code Pck from Microsoft to run this http://archive.msdn.microsoft.com/WindowsAPICodePack/Release/ProjectReleases.aspx?ReleaseId=4906
namespace AndreasCoroiu.Controls
{
public partial class TaskbarThumbnail : UserControl
{
TaskbarForm taskbarForm;
public TaskbarThumbnail()
{
InitializeComponent();
if (!DesignMode)
{
taskbarForm = new TaskbarForm();
TabbedThumbnail preview = new TabbedThumbnail(taskbarForm.Handle, taskbarForm.Handle);
TaskbarManager.Instance.TabbedThumbnail.AddThumbnailPreview(preview);
preview.TabbedThumbnailBitmapRequested += (o, e) =>
{
Bitmap bmp = new Bitmap(Width, Height);
DrawToBitmap(bmp, new Rectangle(new Point(0, 0), bmp.Size));
preview.SetImage(bmp);
e.Handled = true;
};
}
}
public void Show()
{
taskbarForm.Show();
}
private class TaskbarForm : Form
{
public TaskbarForm()
: base()
{
FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
Size = new System.Drawing.Size(0, 0);
}
}
}
}

Tooltip background alternation

The single tooltip which shows different messages for different controls. Now the Problem is the background image is not fit/suitable to all messages. I supposed to call the draw event of the tooltip for custom size, Font etc.,
I able to successfully call the draw and Popup event of the tooltip for particular message but setting the generalized size for different messages(e.ToolTipText) is unknown to me.
public void tooltip_Popup(object sender, PopupEventArgs e)
{
e.ToolTipSize = new Size(100, 100);
}
Kindly let me know anybody have any idea about it.
you can set the size in the Popup event, like this:
private void toolTip1_Popup(object sender, PopupEventArgs e)
{
e.ToolTipSize = new Size(200, 200);
}
result of my test is this, hope it's helpful to you.
I found the answer for my Problem. The below code of POPUP event will change the tooltipsize according to the text size.
public void toolTip_Popup(object sender, PopupEventArgs e)
{
using (Font f = new Font("Arial", 12f))
{
e.ToolTipSize = TextRenderer.MeasureText(
toolTips.GetToolTip(e.AssociatedControl), f);
}

How can i show an image while my application is loading

i have and application windows form .net and my form1 takes a lot of time to appear because in it's event form1_Load does a lot of operation.
My goal is to show an image while the operation are being done.
private void form1_Load(object sender, EventArgs e)
{
methode1();
}
While my methode1() is working, my form doesnt show, i want to show an image on the screen while my methode1() is working because while methode1() is working, there is nothing on the screen.
All the visual things in .net is done on form. You can do it by creating an small form which contains an image load it before module1() and after completing module1() close it. Just below..
private void form1_Load(object sender, EventArgs e)
{
Form f = new Form();
f.Size = new Size(400, 10);
f.FormBorderStyle = FormBorderStyle.None;
f.MinimizeBox = false;
f.MaximizeBox = false;
Image im = Image.FromFile(path);
PictureBox pb = new PictureBox();
pb.Dock = DockStyle.Fill;
pb.Image = im;
pb.Location = new Point(5, 5);
f.Controls.Add(pb);
f.Show();
methode1();
f.Close();
}
Create another form, just for loading, with a static image, and display it before your application starts to load, and destroy it afterwards. Always on top, and with no border is the usual setup for such things.
Try this code
using System.Reactive.Linq;
private void RealForm_Load(object sender, EventArgs e)
{
var g = new Splash();
// place in this delegate the call to your time consuming operation
var timeConsumingOperation = Observable.Start(() => Thread.Sleep(5000));
timeConsumingOperation.ObserveOn(this).Subscribe(x =>
{
g.Close();
this.Visible = true;
});
this.Visible = false;
g.ShowDialog();
}
This code uses Microsoft Rx to execute operations in background threads among other cool features
http://msdn.microsoft.com/en-us/data/gg577609.aspx
In order for this code to work you need to reference two nuget packages: Rx and Rx windows forms
https://nuget.org/packages/Rx-Main/1.0.11226
https://nuget.org/packages/Rx-WinForms/1.0.11226
(splash screen c# -- google it)
Here's what I just found:
http://msdn.microsoft.com/en-us/library/aa446493.aspx
How about using the built in SplashScreen class?
http://msdn.microsoft.com/en-us/library/system.windows.splashscreen.aspx

Categories