|
The easiest way of creating a screenshot is to P/Invoke BitBlt and related GDI functions.
One thing you could try is in your override to OnPaint , call the base.OnPaint first, then draw your custom object. Finally, create a new Bitmap like so:
Bitmap bmp = new Bitmap(this.Size.Width, this.Size.Height, e.Graphics; ...where e is the PaintEventArgs passed to your handler. It's worth a try.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Dear Heath,
Thanks for help but for using bitblt i have to deal with HDC and i would like to make my application independent from the OS,
as for the second way i have try it before it didn't work becuse BitMap constructor use the graphics just for setting the bitmap rosolution and it don't get the drawon ojects from the graphics
<br />
protected override void OnPaint(PaintEventArgs e)<br />
{<br />
e.Graphics.Clear(Color.White);<br />
e.Graphics.DrawLine(Pens.Blue , 0 , 0, 100 , 100 );<br />
Bitmap b = new Bitmap(100 ,100 , e.Graphics );<br />
b.Save("c:\\test.bmp");<br />
{<br />
or
<br />
protected override void OnPaint(PaintEventArgs e)<br />
{<br />
Bitmap b = new Bitmap(100 ,100 , e.Graphics );<br />
e.Graphics.Clear(Color.White);<br />
e.Graphics.DrawLine(Pens.Blue , 0 , 0, 100 , 100 );<br />
b.Save("c:\\test.bmp");<br />
}<br />
both tests gives me blank image
so do you know where is the problem
Regrads
Mhmoud Rawas
------------
Software Eng.
|
|
|
|
|
Well, yes, I know, but you can use that (basically, it creates a compatible bitmap) to draw into as well.That was the intention. Also, don't save with every call to OnPaint - do you realize how much a performance hit you'll take? Use a state variable or something.
Without using BitBlt to source-copy an HDC , about the only other option (because this is a Windows-specific operation) is to create a compatible Bitmap (using a Graphics object from CreateGraphics ), then getting the Graphics object from the Bitmap . This results in about what CreateCompatibleDC does.
You then pass that second Graphics to OnPaint like so:
using (Graphics g1 = CreateGraphics())
{
using (Bitmap bmp = new Bitmap(Bounds.Width, Bounds.Height, g1))
{
using (Graphics g2 = Graphics.FromImage(bmp))
{
PaintEventArgs e = new PaintEventArgs(g2, Bounds);
OnPaint(e);
}
bmp.Save(filename);
}
} This will only capture what you've drawn, however.
Keep in mind that one provider for Windows Forms in Mono is Wine, so all this stuff would still work. This type of functionality is specific to the window manager, though. Some things can't be written genericly (like modifying controls using Windows messaging since you can't always do this using the .NET FCL).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Dear Heath
first of all thanks for giveing me your time,
the last caode which i have wrtie was just for testing i know that it is not applicable to save a picture in a paint event but as l have told you that is was just for testing.
i have tryed your code but as i think it did not do the job i want, actually i have a paint event which will take a long time so i would like to draw it on the form or control bordar then i would like to capture an in=mage from the drawn control to redraw it just for refreshment with out changing the drawing zoom, when i have tryed your sample it just draw the image and dont draw on the control as i want let me give you this -sample-
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.Clear(Color.White);
DateTime dt = DateTime.Now;
for ( int i = 0 ; i < 600 ; i++)
for ( int j = 0 ; j < 600 ; j++)
{
e.Graphics.DrawLine(Pens.Blue , i,j,i+3,j+3);
}
label1.Text = ((TimeSpan)(DateTime.Now - dt)).TotalMilliseconds.ToString() ;
base.OnPaint (e);
}
private void button1_Click(object sender, System.EventArgs e)
{
using (Graphics g1 = CreateGraphics())
{
using (Bitmap bmp = new Bitmap(Bounds.Width, Bounds.Height, g1))
{
using (Graphics g2 = Graphics.FromImage(bmp))
{
PaintEventArgs ee = new PaintEventArgs(g2, Bounds);
OnPaint(ee);
}
bmp.Save("C:\\Tt.BMP");
}
so if you try it you will find that when you click the button it will just draw the image not both image and control,
be carfull i would like from you to test the last sample using control paramater double buffer to false,
also i need something if -and only if- we set double buffer to true how can we reach the buffer where it seems like if we create a fixed HDC for the control and we can draw use it as a source in BitBlt function, so if we can create an image based on that buffer it will be great.
finally for give me for the long replay but i have search for it in the MSDN and i could not get any information about this problem so i appreciate your help
Regards,
Mhmoud Rawas
------------
Software Eng.
|
|
|
|
|
As I mentioned before, if you want to get an actual screenshot you must bitblt the HDC . Taking screenshots is specific to the Windows manager, and there's just no way around it. If you read-up on frame buffers, windowing, and drawing in a window manager you'd understand why.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi,
I have a richtextbox. I want to display a datagrid with values inside the richtextbox.There should be a scroll in the textbox if the datagrid size is larger than that of the textbox. How do I do this ??
Karteek
|
|
|
|
|
You could extend the DataGridColumnStyle with your own class that uses a RichTextBox . Override GetMinimumHeight and return an int large enough to show a decent amount of rich text. There are a couple articles here on CodeProject about creating your own DataGridColumnStyle s; just do a search for "DataGridColumnStyle".
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
i want to implement a simple software firewall by csharp with ability of filtering ip addresses and blocking som of ports
i havent found yet any usefull solution
koosha
|
|
|
|
|
C# is probably a bit high level for that kind of thing. Usually firewalls are implemented deep in OS.
However, a product like WinPKFilter[^] could help as it performs the hooks into the NDIS layer and you can use P/Invoke to process the packets in C#.
Does this help?
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
The Second EuroCPian Event will be in Brussels on the 4th of September
|
|
|
|
|
I think this site maybe lead you to a wonder:
Full firewall source code, please visit: http://www.filseclab.com/eng/products/sourcecode.htm.
|
|
|
|
|
How is this much different from using the NDIS Hook code availble in WinPKFilter that I mentioned?
The source you site is written in C/C++ and would still require pinvoking for it to work with .NET.
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
The Second EuroCPian Event will be in Brussels on the 4th of September
Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!
My Blog
|
|
|
|
|
Hello,
I have a xml-file that looks like this,
<packageimplementation version="1.0">
<companydependancies>
<main>
<tablegroups>
<tablegroup name ="programs">independent</tablegroup>
<tablegroup name ="parameters">independent</tablegroup>
</tablegroups>
</main>
<exeptions company="RLS">
<tablegroups>
<extablegroup name = "settings">independent</extablegroup>
</tablegroups>
<tables>
<extable name ="progmst">independent</extable>
</tables>
</exeptions>
</companydependancies>
</packageimplementation>
Now i wanted to show the tablegroups and the extables in a datagrid, i do it like this:
DataSet ds = new DataSet();
ds.ReadXml(filePath);
dataGrid1.DataSource = ds;
dataGrid1.DataMember = "tablegroup";
DataSet ds2 = new DataSet();
ds2.ReadXml(filePath);
dataGrid2.DataSource = ds2;
dataGrid2.DataMember = "extable";
But this doens't work:
An unhandled exception of type 'System.ArgumentException' occurred in system.data.dll
Additional information: The same table (tablegroups) cannot be the child table in two nested relations.
Why is this wrong and what can i do about it?
Thx in advance
|
|
|
|
|
Hi,
I noticed a problem in the XML which will not allow it to load into the dataset.
the problem is the node "tablegroups" is child of both "main" and "exceptions". So for the dataset to work you need to change one of the name... say "tablegroups_main" and "tablegroups_exp"
regards,
Aryadip.
Cheers !! and have a Funky day !!
|
|
|
|
|
hai there
i have a horscroll bar and a vertical on my form. user may clck once or twice or n number of continues time with out releasing the mouse( click and hold);
i need to cancel all click event and i need to take the last click event.
that is if he click and hold the mouse i need to find the last click value and no of click made on that holding process.
how it is possible
hai, enjoy coding
Sreejith SS Nair
|
|
|
|
|
You should override the WndProc method and handle the WM_HSCROLL (0x0114) and WM_VSCROLL (0x0115). In those cases, if you don't want the scroll operation to occur, do not call base.WndProc . The scroll events will never get to the scroll bar, so it shouldn't scroll.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi, while writing a .NET port of my HTML Reader Class Library[^] using C#, I'm kinda stucked with a stupid problem. You may be knowing that the library allows to read HTML text both from in-memory strings and disk files. At first I declared a private field of type System.IO.TextReader in my LiteHTMLReader class (the main class of the library) and defined a Read function with 1 overload. Some portion of the class looked like this:
public class LiteHTMLReader
{
private System.IO.TextReader oHtmlReader = null;
public long Read(string htmlText)
{
oHtmlReader = new StringReader(htmlText);
return (parseHTMLDocument());
}
public long Read(string pathToFile, System.Text.Encoding encoding)
{
oHtmlReader = new StreamReader(pathToFile, encoding, true);
return (parseHTMLDocument());
}
}
But soon enough, I learned that readers (derived from System.IO.TextReader ) are just forward-only. But sometimes while parsing an HTML document, I need to move back also. So, I rejected the idea of using readers. So obviously, my next option was using streams. Ok now we have a System.IO.FileStream class to deal with files, but what about the string s.
- How can I open a stream on a string?
- Is there any class available in the framework?
- Shall I go for my own implementation?
- Any other better option availble for the above-defined scenario?
Please suggest.
Even if I use streams, there is one more thing I need to know. Stream s deal with bytes only (GetBytes ). String class uses Unicode by default. How to deal with this situation?
Guys, I'm so sorry for asking soooooo much but you can obviosly guess that I'm a newbie in C#. And I'm getting mails daily from different people requesting me to release a .NET port of the library. Please help.
Any suggestions are welcome.
Regards,
Gurmeet BTW, can Google help me search my lost pajamas?
My Articles: HTML Reader C++ Class Library, Numeric Edit Control
|
|
|
|
|
xsl transform to stream adds extra characters
That question indicates you can use MemoryStream for string streams. Hope that helps.
regards,
Paul Watson
Bluegrass
South Africa
Chris Maunder wrote:
"I'd rather cover myself in honey and lie on an ant's nest than commit myself to it publicly."
Jon Sagara replied:
"I think we've all been in that situation before."
Crikey! ain't life grand?
|
|
|
|
|
... but I'm not sure if its the right one. Now my class looks like this:
public class LiteHTMLReader
{
private System.IO.Stream oHtmlStream;
public long Read(string htmlText)
{
using (this.oHtmlStream = new System.IO.MemoryStream(System.Text.Encoding.Unicode.GetBytes(htmlText)))
{
long lCharCount = this.parseHTMLDocument();
return (lCharCount);
}
}
public long ReadFile(string pathToFile)
{
using (System.IO.StreamReader sr = new System.IO.StreamReader(pathToFile, true))
{
string strFileData = sr.ReadToEnd();
return (this.Read(strFileData));
}
}
} I dunno why but I dont think this is the actual way to do it. Can someone clear my doubts?
Heath Stewart, Mike Dimmick, any other C# guru, where are you guyz? Please help.
Thanks,
Gurmeet BTW, can Google help me search my lost pajamas?
My Articles: HTML Reader C++ Class Library, Numeric Edit Control
|
|
|
|
|
You can use the Encoding class to convert the bytes to strings using the appropriate encoding.
Also, even with a reader, you should be able to call Seek on the BaseStream property if you use a StreamReader (which derives from TextReader ).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
If you do Seek on the BaseStream , remember to call DiscardBufferedData on the StreamReader . Otherwise, you'll get the rest of the buffered data before you get the data from the new position in the stream.
It doesn't look like you can turn this buffering off.
Stability. What an interesting concept. -- Chris Maunder
|
|
|
|
|
Your solution suggests using BaseStream, property of StreamReader class. But I don't only need to deal with files, I need to deal with strings also. The StreamReader class has a BaseStream property but the StringReader class does not. What do you suggest in this case?
Moreover, I would like to ask you whether the "alternate way" that I've posted above is right according to you in this situation or not.
Thanks,
Gurmeet BTW, can Google help me search my lost pajamas?
My Articles: HTML Reader C++ Class Library, Numeric Edit Control
|
|
|
|
|
There's almost never a "right" way, just good and bad ways. Your alternative - if it works - isn't bad and seems to be pretty efficient. That's what counts.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
|
Sorry, that was supposed to be "efficient", not "inefficient" ( I use the latter far more often here in this forum ). Context clues should've told you that, but thanks for the low vote anyway.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Can i create a split window with the LHS consisting of various tabs and on the RHS have buttons that will jump to different tabs? How would I go about doing this?
|
|
|
|
|