|
using (Bitmap img = new Bitmap(MaxSize, MaxSize))
{
for (int i = 0; i < MaxSize; i++)
{
for (int j = 0; j < MaxSize; j++)
{
if (obj.Img_In_Process[i, j] < 0) img.SetPixel(j, i, Color.FromArgb(0, 0, 0));
else img.SetPixel(j, i, Color.FromArgb(255, 255, 255));
}
}
picbox.Image = img;
//string path = Application.ExecutablePath + "\\result.bmp";
//MessageBox.Show(path);
img.Save("C:\\result.bmp");
//img.Save(path);
}
will always dispose of your image when the scope ends.
I would suggest you read my image processing articles for info on how to efficiently do this sort of operation.
And, I would need to see the stack trace to get an idea of why you are getting an exception. Does result.bmp exist ? Can you fix the exception by deleting it ? My first guess would be that you have that image open somewhere else, and so cannot save over it, but I'd expect the message to be different.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Hi,
you should not dispose of the image when it is still in use; it is in use
thru your picbox.Image = img; line.
If you want to get rid of the image, first do picbox.Image = null;
or something like that.
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
Which line does the exception get thrown?
|
|
|
|
|
Thanks for your help Jordanwb, I got message at this line:
<br />
...<br />
static class Program<br />
{<br />
[STAThread]<br />
static void Main()<br />
{<br />
Application.EnableVisualStyles();<br />
Application.SetCompatibleTextRenderingDefault(false);<br />
Application.Run(new frmMain()); <<<<< This line<br />
}<br />
}<br />
|
|
|
|
|
OK, that means that the exception is throw outside your try block, and is not caught anywhere inbetween. What's the stack trace ? What happens if you step through your code to see what line blows up ?
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
OK, you're right! Thanks you so much Christian Graus, I've fixed it
Best regards!
|
|
|
|
|
One more simple question (but difficult for me!!):
How to save the image file into the folder, which contains my project??
I have tried to use
<br />
string path = Application.ExecutablePath + "\\result.bmp";<br />
img.Save("C:\\result.bmp");<br />
But path is wrong ("\\" not "\" for location address of file). Thank you.
|
|
|
|
|
Path.Combine(Application.StartupPath, "result.bmp")
Of course, your code snippet will still save to C for obvious reasons, but the ExecutablePath contains the filename, too. Path.GetDirectory will strip a filename if you ever need to.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Thanks you so much, I understood already.
|
|
|
|
|
Um your welcome.
|
|
|
|
|
Hi,
I want to calculate this formula upon radian, not degree. (because I want to use it in an Area formula)
teta - sin(teta)
that : teta = 2*ArcCos(d)
I'd wrote this but it's wrong:
(2 * (Math.Acos(d) * 180 / Math.PI)) - Math.Sin((2 * (Math.Acos(d) * 180 / Math.PI)) * Math.PI / 180)
what's the correct?
Best wishes
|
|
|
|
|
Isn't 180/PI how you convert to degrees ? Either way, you just do whatever conversions you need to get it to where you want it.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
As said above, you can convert from degrees to radians by multiplying by PI/180. To go from radians to degrees, you multiply by 180/PI.
|
|
|
|
|
What is the easiest way to compare two array lists?
I´m making a spelling test.
|
|
|
|
|
Depends on what kind of data is inside the ArrayList, can you give us more information?
modified 12-Sep-18 21:01pm.
|
|
|
|
|
strings.
al.Add("str1");
al.Add("str2");
al.Add("str3");
al.Add("str4");
al.Add("str5");
al2.Add(richTextBox1.Text);
al2.Add(richTextBox1.Text);
al2.Add(richTextBox1.Text);
al2.Add(richTextBox1.Text);
al2.Add(richTextBox1.Text);
|
|
|
|
|
Do you want to compare them one by one, like this? =>
value0 = comapare(a1[0], a2[0]);
value1 = comapare(a1[1], a2[1]);
value2 = comapare(a1[2], a2[2]);
value3 = comapare(a1[3], a2[3]);
Then I would suggest you take a look at the Levenshtein Distance[^], which is a very good algorithm for comparing strings like used in spell checking programs or search engines.
You can find a very fast C# implementation here[^]
If you use .NET 2.0 then I'd also suggest you use generics, so you don't have to cast your objects to string s when accessing the list. Use List<string> instead
regards
modified on Tuesday, April 22, 2008 3:21 PM
modified 12-Sep-18 21:01pm.
|
|
|
|
|
ALright, the thing is I have no idea how to use the Levenshtein Distance.
|
|
|
|
|
You can use the source from the link I posted, here's an example:
int differenceBetweenTwoWords = YetiLevenshtein(a1[index], a2[index]);
modified 12-Sep-18 21:01pm.
|
|
|
|
|
You want to compare one word to the words in an ArrayList? Then there is no reason to repeat the word in an ArrayList. That won't help you the least bit in comparing them. Just loop though the list of words and compare them to the single word.
Unless you are stuck with framework 1.x, don't use an ArrayList at all. Use a List<string> instead.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
So I don´t need any complicated algorithms?
Could you give an example.
|
|
|
|
|
Doesn't the ArrayList have a Contains method ? So you check if the ArrayList contains each word, one at a time.
Why are you using .NET 1.1 ?
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
bool found = false;
string word = richTextBox1.Text;
foreach (string w in al) {
if (w == word) {
found = true;
break;
}
}
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
dennycrane wrote: I´m making a spelling test.
If each of the words were spelled in the text box on it's own line, e.g.
[Textbox looks like this]
Foo
Bar
Bat
Baz Then I'd code it up like this:
string[] correctlySpelledWords = { "foo", "bar", "bat", "baz" };
string[] studentSpelledWords = textBox.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
bool studentSpelledEverythingCorrectly = ContainsSameElements(correctlySpelledwords, studenSpelledWords);
....
bool ContainsSameElements(string[] first, string[] second)
{
if (first.Length != second.Length)
{
return false;
}
for (int i = 0; i < first.Length; i++)
{
if (!string.Equals(first, second, StringComparison.InvariantCultureIgnoreCase))
{
return false;
}
}
return true;
} That ignores casing (e.g. spelling "Foo" as "foo" is acceptable).
FWIW, you can probably do this in less code using a LINQ query if you're using the latest version of .NET.
|
|
|
|
|
so this is what I have
don´t I need a list to compare though?
private void button1_Click(object sender, EventArgs e)
{
AthugaStafset();
ContainsSameElements(first, second);
}
public void ChkSpell()
{
string[] correctlySpelledWords = { "foo", "bar", "bat", "baz" };
string[] studentSpelledWords = richTextBox1.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
bool studentSpelledEverythingCorrectly = ContainsSameElements(correctlySpelledWords, studentSpelledWords);
if (studentSpelledEverythingCorrectly)
{
label1.Text = "Same";
}
else
{
label1.Text = "They differ";
}
}
public bool ContainsSameElements(string[] first, string[] second)
{
if (first.Length != second.Length)
{
return false;
}
for (int i = 0; i < first.Length; i++)
{
if (!string.Equals(first, second, StringComparison.InvariantCultureIgnoreCase))
{
return false;
}
}
return true;
|
|
|
|