|
I am new to C# and I thank everyone who helped me on my silly questions.
and here is one more:
the following code is going to draw a selected image in a picturebox control,
however, since I use graphics.drawimage, the picture will not be redrawn if covered by something else.
may someone please help me?
private void menuItem2_Click(object sender, System.EventArgs e)
{
Point myloc=new Point(0,0);
Size mysize=new Size(640,480);
Rectangle position =new Rectangle(myloc,mysize);
ofd.Filter="Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*";
if (ofd.ShowDialog()==DialogResult.OK)
{
fileio.filetxt=ofd.FileName;
Bitmap mybmp=new Bitmap(fileio.filetxt);
Graphics g=pictureBox_cur.CreateGraphics();
g.DrawImage(mybmp,position);
}
}
thanks everybody
|
|
|
|
|
use
g.Dispose()
at the end when using resources like images with graphics object.
PAresh;)
|
|
|
|
|
Since you are only painting/drawing the image once, the PictureBox will redraw over it with whatever contents it knows about, if any. That is, the PictureBox has no way to know that you painted a specific image in its client area, so it cannot redraw it when the system tells it to repaint its contents.
I assume you have a specific reason not to just set the PictureBox 's Image property, and instead need to custom draw. Perhaps the easiest way would be to handle the control's Paint event, as you did with menuItem2's Click event. VS.Net will create a function like:
private void pictureBox_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawImage(mybmp, position);
}
You will need to store mybmp and position somewhere the Paint handler can see it, and keep the code in your menuItem2_Click handler that sets the Bitmap and Rectangle. Since the Graphics object is already supplied by the PaintEventArgs object here, do not destroy it.
Windows will fire your Paint handler anytime the control needs to be redrawn.
Cheers
|
|
|
|
|
if I do:
private void pictureBox_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawImage(mybmp, position); //put your drawing code here
}
the system will try to paint mybmp when the form is loaded and create an error
System.NullreferenceException
|
|
|
|
|
I anticipated that problem, but left that out since my post was already getting long. You will need to put error handling in the code such as:
private void pictureBox_Paint(object sender, PaintEventArgs e)
{
if (mybmp != null)
{
e.Graphics.DrawImage(mybmp, position);
}
}
and where the variables are declared, such as in your form's code:
private Bitmap mybmp=null;
private Rectangle position;
That should take care of the errors you got. This way, only when you have loaded a bitmap, will one be drawn.
Cheers
|
|
|
|
|
this works if i just wanna draw a Bmp. what if I draw something else.
Shall I always preserve what i draw in a bmp and repaint the bmp when needed?
|
|
|
|
|
clarkwuzhe wrote:
what if I draw something else.
Shall I always preserve what i draw in a bmp and repaint the bmp when needed?
That depends on what you ultimately want to do. If there are only a small number of formats which your app might draw, then you could keep a variable around for each type, and yet another one that tells your Paint handler which one to render. That would be useful if you need to save (or otherwise preserve) that exact graphics format. However, that doesn't seem likely based on your original question.
Otherwise, it might be easier, as you mentioned, to convert each type to a Bitmap (which is probably the most universal and flexible type to use with GDI+), and always draw whatever's in mybmp . Behind the scenes, GDI+ will always convert an image to bitmap format (if necessary) for the actual rendering anyway.
Like I posted earlier, I do not know why you are bypassing PictureBox 's Image property, which will handle the Paint requests automatically. As it stands, you could use a Label (or most any other control) and draw using its Graphics object too. PictureBox already contains methods for fairly custom image drawing, which you do not need if you are custom drawing yourself. Just letting you know; I don't mean to confuse you.
Cheers
|
|
|
|
|
I have asked this question before...but never got to many posts. im wondering how i can get a good start in the software dev field (in C#) i am enrolled in a 6 month tech school (starting next month on the 9th) to get my MCSD...after which the school helps you find a job...what other things would you recommend for me to be doing in addition to getting my MCSD ?
Jesse M
The Code Project Is Your Friend...
|
|
|
|
|
Experience. No-one cares about whatever whiz-bang work you may have done at home. Or even at school to a large degree.
Try find some part-time work even vaguely related to the industry or even volunteer somewhere. Even it's just crappy work that has no value, it's something you can put on your CV and bullshit about in the interview. And it shows that you have initiative.
he he he. I like it in the kitchen! - Marc Clifton (on taking the heat when being flamed)
NEW: Awasu v0.7[^]: A free RSS reader with support for Code Project.
|
|
|
|
|
yeah...i'm currently looking to develop...anything with someone.....do you (or anyone) have anything you are looking for help with...however meaningless...the job is... im the man for it..lol
Jesse M
The Code Project Is Your Friend...
|
|
|
|
|
Try taking a look on sourceforge for something that takes your fancy.
There are always organizations around that need people to help out even if it's not necessarily computer-related. You can boost your CV *and* do something socially useful at the same time
he he he. I like it in the kitchen! - Marc Clifton (on taking the heat when being flamed)
NEW: Awasu v0.7[^]: A free RSS reader with support for Code Project.
|
|
|
|
|
whats the address for souce forge? (http://www.sourceforge.com) ? thanks alot for the usefull info
The Code Project Is Your Friend...
|
|
|
|
|
It's actually sourceforge.net but either one will work.
welcome to the wunnerful world of open source...
he he he. I like it in the kitchen! - Marc Clifton (on taking the heat when being flamed)
NEW: Awasu v0.7[^]: A free RSS reader with support for Code Project.
|
|
|
|
|
say you pick one files in the openfiledialogue window. how can u get all the files names in a string array?
|
|
|
|
|
using System;
using System.IO;
using System.Collections;
// Takes an array of file names or directory names on the command line.
// Determines what kind of name it is and processes it appropriately
public class RecursiveFileProcessor {
public static void Main(string[] args) {
foreach(string path in args) {
if(File.Exists(path)) {
// This path is a file
ProcessFile(path);
}
else if(Directory.Exists(path)) {
// This path is a directory
ProcessDirectory(path);
}
else {
Console.WriteLine("{0} is not a valid file or directory.", path);
}
}
}
// Process all files in the directory passed in, and recurse on any directories
// that are found to process the files they contain
public static void ProcessDirectory(string targetDirectory) {
// Process the list of files found in the directory
string [] fileEntries = Directory.GetFiles(targetDirectory);
foreach(string fileName in fileEntries)
ProcessFile(fileName);
// Recurse into subdirectories of this directory
string [] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
foreach(string subdirectory in subdirectoryEntries)
ProcessDirectory(subdirectory);
}
// Real logic for processing found files would go here.
public static void ProcessFile(string path) {
Console.WriteLine("Processed file '{0}'.", path);
}
}
This program will allow you to select all the files and subdirectory
once you pass the folder name
Paresh
|
|
|
|
|
you could find your solutions also at
http://www.gotdotnet.com/Community/MessageBoard/MessageBoard.aspx?ID=6
thanx
Paresh;P
|
|
|
|
|
Is there anything like isnumber("1")=true?
or something else?
thanks
|
|
|
|
|
string s = "1";
int n = Convert.ToInt32(s);
Paresh
|
|
|
|
|
however,
string s="a";
int n=Convert.toint32(s);
will give an error, won't it? there must be a way to tell...
|
|
|
|
|
|
if you want comparision
then
string s = "1";
if (s.CompareTo("1") == 0 )
{
// blah
}
Paresh
|
|
|
|
|
i found a discussion here:
http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_20391710.html
but i really can't believe this language leads to such a pain in the a$$
|
|
|
|
|
see, i just gave u the temp. working solution. i believe there are tons of solution for this type of problem
u could check char.by char.,
regex, string - try-catch method, int.parse, etc...
Paresh
|
|
|
|
|
Use this:
public static bool IsNumber(string strNumber)
{
Regex objNotNumberPattern=new Regex("[^0-9.-]");
Regex objTwoDotPattern=new Regex("[0-9]*[.][0-9]*[.][0-9]*");
Regex objTwoMinusPattern=new Regex("[0-9]*[-][0-9]*[-][0-9]*");
String strValidRealPattern="^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";
String strValidIntegerPattern="^([-]|[0-9])[0-9]*$";
Regex objNumberPattern =new Regex("(" + strValidRealPattern +")|(" + strValidIntegerPattern + ")");
return !objNotNumberPattern.IsMatch(strNumber) &&
!objTwoDotPattern.IsMatch(strNumber) &&
!objTwoMinusPattern.IsMatch(strNumber) &&
objNumberPattern.IsMatch(strNumber);
}
Deepak Kumar Vasudevan
http://deepak.portland.co.uk/
|
|
|
|
|
If this was VB.NET, you could use IsNumeric, so you could wrap that up in a class that is callable from C#.
Another C# approach would probably be to check each character in the string with IsDigit or IsNumber, or maybe use something similar to the following.
string s="1234";<br />
bool b = s.Trim("1234567890".ToCharArray()).Equals("");
--
Ian Darling
|
|
|
|