|
Hi friends,
I am designing a paint brush type program…..for drawing area where I can draw something then I used pictureBox…..like as
Pen p = new Pen(Color.Red, 5);<br />
g = pictureBox2.CreateGraphics();<br />
g.DrawEllipse(p, e.X, e.Y, 5, 7);
in this program I can draw anything but when I want to save this drawing by the help of this command: pictureBox2.Image.Save(saveFileDialog1.FileName);
then an error occur : “NullReferenceExpection was unhandled !” “Object reference not set to an instance of an object .”
i got the solution of above problem like as:
Bitmap bmp = new Bitmap(pictureBox2.Width, pictureBox2.Height);<br />
Graphics g = Graphics.FromImage(bmp);<br />
Pen p = new Pen(Color.Red, 5);<br />
g.DrawEllipse(p, e.X, e.Y, 5, 7); <br />
pictureBox2.Image = bmp;
follwing code solve just half problem...............means save only last ellipse ,but i want whole drawing
private void pictureBox2_MouseDown(object sender, MouseEventArgs e)<br />
{ <br />
if (e.Button == MouseButtons.Left)<br />
{<br />
Bitmap bmp = new Bitmap(pictureBox2.Width, pictureBox2.Height);<br />
<br />
Graphics g = Graphics.FromImage(bmp);<br />
<br />
Pen p = new Pen(Color.Red, 5);<br />
<br />
g.DrawEllipse(p, e.X, e.Y, 5, 7); <br />
} ---------------------------------------------------------------
and then ......... ... .
if (saveFileDialog1.ShowDialog() == DialogResult.OK)<br />
{<br />
pictureBox2.Image = bmp;<br />
<br />
pictureBox2.Image.Save(saveFileDialog1.FileName);
}-----------------------------------------------------------------
i think, there is something missing in
Graphics g = Graphics.FromImage(bmp);
or
pictureBox2.Image = bmp; (like: pictureBox2.Image += bmp; but illegal )
-----------------------------------------------------------------
bmp or pictureBox does not update with previous condition
it always losing the previous ellipse and save the last drawn
ellipse
i need your help!
thanks
|
|
|
|
|
Hello,
I think you have to do the drawing task in the OnPaint event of the PictureBox.
To force painting of the control you could use "Refresh()" or "Invalidate()".
And you should allways dispose the IDisposable members.
Cause this will cause memory leaks!
Pen p = new Pen(Color.Red, 5);
p.Dispose();
or
using(Pen p = new Pen(Color.Red, 5))
{
}
All the best,
Martin
|
|
|
|
|
Explain more.......
i use
private void pictureBox2_MouseDown(object sender, MouseEventArgs e)<br />
private void pictureBox2_MouseMove(object sender, MouseEventArgs e)<br />
private void pictureBox2_MouseUp(object sender, MouseEventArgs e)
events for drawing onto the pictureBox
how i can use "Refresh()" or "Invalidate()"?
i just want drawing area(pictureBox,bitmap etc..) where i draw something and save this drawing in my hard drive.
i already make the paintbrush type program ,all functions doing well ....also drawing is save onto the my drive but it is incomplete
|
|
|
|
|
Hi Martin,
Martin# wrote: And you should allways dispose the IDisposable members
you should Dispose() the disposable objects you created, either by instantiating
them with the new keyword, or by calling a create method such as CreateGraphics;
but not the ones handed to you, e.g. PaintEventArgs.Graphics or the system objects
such as Pens.Black
|
|
|
|
|
Hello Luc,
Thanks for the additional infos, I didn't explained it propper I guess!
All the best,
Martin
|
|
|
|
|
zeeShan anSari wrote: g = pictureBox2.CreateGraphics();
Out of curiosity, what made you do it like this ? There seem to be a flood of people doing this, and it's totally wrong.
zeeShan anSari wrote: in this program I can draw anything but when I want to save this drawing by the help of this command: pictureBox2.Image.Save(saveFileDialog1.FileName);
This was answered at length for someone else just below. CreateGRaphics is bad, don't use it. If you use a picturebox, put a picture in it, and draw on it first. Then you can save it. CreateGraphics should be used only for drawing things you want to be able to erase, such as rubber bands. It is not for persistent drawing, and certainly it makes your picture box a waste of time, it never does anything.
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Christian Graus!
i change my codeing
Bitmap bmp = new Bitmap(pictureBox2.Width, pictureBox2.Height);
Graphics g = Graphics.FromImage(bmp);
Pen p = new Pen(Color.Red, 5);
g.DrawEllipse(p, e.X, e.Y, 5, 7);
pictureBox2.Image = bmp;
here i don't use CreateGraphics();
------------
please read my first post " Problem about bitmap in Visual Studio C Sharp.NET ?"
|
|
|
|
|
Dear All,
I have two grids g1 and g2 and three tables t1,t2,t3. Basically there are only two tables t1 & t3 which have many to many relationship between them for which i have created t2. I want to show the data of t1 in g1 and data of t3 in g2 based on selected row of g1.
Can any body tell me how can i do this? Currently i have created two relationships dr1 (between t1 & t2) and dr2 (between t2 & t3) and have databinding like this:
g1.DataSource=t1;
g2.DataSource=t1;
g2.DataMember="dr1.dr2";
This scheme shows the data in g2 but not all the relevant rows in g2 are displayed. Instead just a single row against one row in g1 is displayed.
Kindly give me some guidance in this regard.
Regards,
Wasif Ehsan.
|
|
|
|
|
I'd go the easy way out & create two datasets that collect the information I want to display & bind the grids to them.
I think I'm going to call my next project "Chuck Norris". It's a sure way to guarantee it's unbreakable.
• • •
But fortunately we have the nanny-state politicians who can step in to protect us poor stupid consumers, most of whom would not know a JVM from a frozen chicken. Bruce Pierson
|
|
|
|
|
I am working on an application in which i save save html files which have ISO-8859-1 encoding (The html files are in spanish).
I use stream writer to save the source of the files as html files.
The problem is the encoding options available in the system.text.Encoding class are
ASCII
UTF7
UTF32
UTF8
Unicode
so when I save the page it changes certain characters.
Can Any 1 suggest which encoding i should use ...
o O º(`'·.,(`'·., ☆,.·''),.·'')º O o°
»·'"`»* *☆ t4ure4n ☆* *«·'"`«
°o O º(,.·''(,.·'' ☆`'·.,)`'·.,)º O o°
|
|
|
|
|
Encoding.Default should do the job
|
|
|
|
|
Thanks it worked for me
o O º(`'·.,(`'·., ☆,.·''),.·'')º O o°
»·'"`»* *☆ t4ure4n ☆* *«·'"`«
°o O º(,.·''(,.·'' ☆`'·.,)`'·.,)º O o°
|
|
|
|
|
Those are only the predefined encodings. You can create an encoding for any code page that the system supports using the Encoding.GetEncoding method. Check the documentation for the Encoding class for all the available encodings.
---
single minded; short sighted; long gone;
|
|
|
|
|
How to save into a file a graphics?
I tried this but not working
Graphics g = pictureBox1.CreateGraphics();
Pen p = new Pen(Color.Black);
g.DrawRectangle(p, 1, 1, 100, 20);
Bitmap bit = new Bitmap(482, 150, g);
bit.Save("c:\\image.bmp");
And if i draw something into a picturebox, how to save it into a file?
I tried this but not working
Graphics g = pictureBox1.CreateGraphics();
Pen p = new Pen(Color.Black);
gg.DrawRectangle(p, 1, 1, 100, 50);
gg.DrawRectangle(p, 2, 10, 50, 30);
Bitmap bb = new Bitmap(pictureBox1.Image);
bb.Save("c:\\image.bmp");
Help me please!
http://pmartike.deviantart.com/
|
|
|
|
|
Standard question #1:
What do you mean by "not working"?
Standard question #2:
What error message do you get?
The CreateGraphics method creates a Graphics object for drawing on the control. There is no specific CreateGraphics method that creates a Graphics object for drawing on the image in a PictureBox . You are just drawing on the screen, that does not affect the image in the PictureBox .
You have to create a Graphics object for the bitmap so that you can draw the graphics on the bitmap instead of on the screen.
---
single minded; short sighted; long gone;
|
|
|
|
|
Thanks, you're right
i solved the problem...
http://pmartike.deviantart.com/
|
|
|
|
|
pmartike wrote: pictureBox1.CreateGraphics();
To further clarify, you should never do this
1 - it's a waste of a picture box, the picture box does nothing
2 - if your form is obscured, what you drew will be erased
Draw in your paint event, or create a Bitmap and assign it to the picture box. Draw on it first if you want to, and save it, if you want to do that.
pmartike wrote: Bitmap bb = new Bitmap(pictureBox1.Image);
The issue here is that there is no image inside the picture box, you need to put one in there, and even if you do, CreateGraphics won't do anything to it. It's more like you are drawing on a layer above the control, a layer that is not permanent.
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Hi,
your & my problem both are same ,read my today(june,27) post
"Problem about bitmap in Visual Studio C Sharp.NET ?"
i solve my half problem by following code:
Bitmap bmp = new Bitmap(pictureBox2.Width, pictureBox2.Height);<br />
Graphics g = Graphics.FromImage(bmp);<br />
Pen p = new Pen(Color.Red, 5);<br />
g.DrawEllipse(p, e.X, e.Y, 5, 7); <br />
pictureBox2.Image = bmp;
*first read my post, if you get any solution then please inform me
|
|
|
|
|
i can't find your post
i resolved my problem too:
<br />
Bitmap bit = new Bitmap(244, 200);<br />
Graphics g = Graphics.FromImage(bit);<br />
g.DrawRectangle(WhitePen, 10,10, 100, 50);<br />
bit.Save("c:\\image.bmp"); <br />
Can you give a link with your post?
http://pmartike.deviantart.com/
|
|
|
|
|
|
When shd we make function static in C#..
We will be able to call function using the class Name. Wat is the advantages of that and wat are the scenarios where we would use static functions
|
|
|
|
|
Sounds like a homework/interview question.
Google for the answers, and if you still don't understand something, be specific with your quesion.
Cheers,
Vıkram.
After all is said and done, much is said and little is done.
|
|
|
|
|
Tell me a scenario where we are forced to make a function static
|
|
|
|
|
Hello,
I think you really should do what have been suggested bevor!
Is the reason why you posted the question again (which is very rude for the one who tried to help you) that you haven't understood the answer?
I give you a help to search for at google:
"Singleton"
All the best,
Martin
|
|
|
|
|
I have this suspicion you thanked me with that 1 vote, but I'll give you the benefit of the doubt.
To answer your question: Singletons. Factory methods. Helper classes. Lots of other scenarios where your function should/does not need to do anything with the instance variables.
Cheers,
Vıkram.
After all is said and done, much is said and little is done.
|
|
|
|
|