Problem with e.Graphics.DrawImage() - c#

This is my code and when I call this code from localhost, printer worked,but from anoder
IP noting.I think DrawImage function not worked.Who can help me.I check the Bitmap object properly created and Image exist.
protected void printButton__Click(object sender, EventArgs e)
{
System.Drawing.Printing.PrintDocument document =
new System.Drawing.Printing.PrintDocument();
document.PrintPage += new PrintPageEventHandler(document_PrintPage);
document.Print();
}
void document_PrintPage(object sender, PrintPageEventArgs e)
{
string s = Server.MapPath("Temp.jpg");
Bitmap objBmpImage = new Bitmap(s);
e.Graphics.DrawImage( objBmpImage, 10, 10, 200, 100);
}

It seems you are in asp.net environment (Server.MapPath) & are assuming that you can get hold of a printer on the client machine.
If you are trying to do that, it cannot be done.
On the other hand, you can't use Server.MapPath in a winforms environment.

Related

EmguCV not reading camera

I have the the following code in a normal windows form application with EmguCV 3.1
public Form1()
{
InitializeComponent();
_capture = new Capture("http://root:pass#192.168.1.27:80/axis-cgi/mjpg/video.cgi");
_capture.ImageGrabbed += ProcessFrame;
}
private void Form1_Load(object sender, EventArgs e)
{
_capture.Start();
}
private void ProcessFrame(object sender, EventArgs e)
{
Mat image = new Mat();
_capture.Retrieve(image);
imageBox1.BackgroundImage = image.Bitmap;
}
I have tested the above link in a browser it worked, I have also tested this using iSpy it also works there but using EmguCV ProcessFrame is never reached
I have also tried to connect to the camera using Luxand and it worked well but Luxand is not free so I have to use EmguCV to do face detection & recognition
Looking at this post, try adding ?x.mjpeg after .cgi in your url.

How can I write a live MetroTile custom control?

Using MetroFramework 1.3.5 and .NET Transitions, in Windows Forms, I have written the code for a live MetroTile. Here is the Timer Tick method used to update the tile:
private void updateTiles_timer_Tick(object sender, EventArgs e)
{
metroPanel1.BackColor = Color.Transparent;
Bitmap bm = new Bitmap(metroPanel1.Width, metroPanel1.Height);
metroTile_startStop.DrawToBitmap(bm, new Rectangle(metroTile_startStop.Location.X, metroTile_startStop.Location.Y, metroTile_startStop.Width, metroTile_startStop.Height));
metroPanel1.BackgroundImage = bm;
if (animationFlag)
{
metroTile_startStop.Text = Properties.Resources.startStopTile_alternateText;
animationFlag = false;
}
else
{
metroTile_startStop.Text = "Start";
animationFlag = true;
}
Transition.run(metroTile_startStop, "Top", metroTile_startStop.Height - 16, 4, new TransitionType_Linear(500));
}
private void metroPanel1_Click(object sender, EventArgs e)
{
metroTile_startStop.PerformClick();
}
The tile is placed inside a MetroPanel with the Size(tile.Width + 10, tile.Height + 8). metroPanel1_Click() is necessary if, during a transition, the user clicks the background bitmap.
I don't have sufficient experience and I need help in writing a custom control so I can easily reuse it in future projects. The control should have a property which sets the timer interval and the possibility to set the tile texts. If I have a working code, then I can extend it to set the tile image, customize animations etc.
Thank you!

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 ?

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

DrawToBitmap on Panel is blank

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!!

Categories