|
For starters:
It should not use a destructor to close a file stream because destruction of the object is non-deterministic. It will hold onto the open file stream until the GC decides to collect it which might not happen before the application closes. It should implement IDispose instead.
|
|
|
|
|
If you can't answer this then you don't deserve a career doing this.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
I'm curious as to what the question was
I get all the news I need from the weather report - Paul Simon (from "The Only Living Boy in New York")
|
|
|
|
|
I wondered that myself when I cruised through this forum earlier.
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
Me too! I wished they wouldn't do that...
"More functions should disregard input values and just return 12. It would make life easier." - comment posted on WTF
|
|
|
|
|
I wished they couldn't do that as soon as there is some reply present.
|
|
|
|
|
Malcolm Smart wrote: Me too! I wished they wouldn't do that...
I guess the poster felt shamed into deleting their message after being told twice that they don't deserve a career as a programmer. Must have been a helluvah post to get that kind of response.
Personally, I wouldn't take on the responsibility of telling someone that regardless of how lamebrained I found their question to be.
|
|
|
|
|
I rather suspect that the OP was in the interview at the time. See my explanation further up the thread as to what the question was that prompted the vitriol.
As to why it was suggested that they weren't suited to a career in programming; they couldn't see obvious deficiencies in a 12 line piece of code. It was a basic test and they just didn't have a clue - would you really want to work with somebody like this. I wouldn't expect to become a plumber with my level of knowledge in plumbing and I don't see why we should put up with people wanting to become programmers when they don't have the basics down.
Now, if the poster goes away and comes back with improved coding skills then I would be delighted.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
|
They immediately get nominated for a Darwin award.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
This is the normal behaviour:
When you double-click an the Visible property of a UserControl , Visual Studio will emboldens the altered property, and enable the Undo menu item, and even prompt you to save if you attempt to close - all indicating that a change has been made.
But during DesignMode :
When I programmatically (using ControlDesigner ) change the Visible property of a MyUserControl , there is no indication that a change has been made, and Visual Studio does not save the changes. Even though I can see that the property value has changed, it doesn’t go bold or get serialized/saved.
How do you fix it please.
If I'm missing some detail, please let me know.
Any comments are welcomed.
Soog.
|
|
|
|
|
Hello,
A bold entry at design time property, means that the actual value is != the DefaultValue (Which is an Attribute on the property).
It also means, that this property is written in the "InitializeComponents()".
This behaviour is only possible if you change the properties value over the Designer, it will have no effect id you change it programmaticaly.
All the best,
Martin
|
|
|
|
|
Thank you for the prompt reply, Martin.
But, there most be a way to force the property to a Dirty state, and make the IDE serialize/save it. This behaviour is vital to the TabControl that I'm working on, because the Visible property of a currently selected TabPage most be programmatically changed through the TabControlDesigner .
Again, any comments are appreciated.
Thanks in advance,
Soog.
|
|
|
|
|
Hello,
I think in this case:
TabControl, saves the SelectedIndex, not the Visible states of the TabPages
All the best,
Martin
|
|
|
|
|
I see, I guess.
The thing is, I don't know how to go about implement this . I've been trying, but don't quite understand it .
Thanks again,
Soog.
|
|
|
|
|
I think, you have to explain what you want to do a little more detailed.
Maybe show us some codesnipped, what you have done so far!
All the best,
Martin
|
|
|
|
|
Yata!
I did it .
The solution, in one word (or phrase) was DesignerTransaction .
After inspecting the Add and Remove events in my DesignerControl , I devised what you can see in the following snippets, which works perfectly:
[Designer(typeof(MyTabControlDesigner))]
public class MyTabControl : UserControl
{
protected override void OnMouseDown(MouseEventArgs e)
{
if ((tabPageNext = GetHitTest(e.Location)) != null)
{
tabPageLast = TabPageFocused;
if (DesignMode)
{
ISelectionService s = (ISelectionService)GetService(typeof(ISelectionService));
ArrayList a = new ArrayList();
a.Add(tabPageNext);
s.SetSelectedComponents(a);
}
else
OnSelectionChanged();
}
base.OnMouseDown(e);
}
internal void OnSelectionChanged()
{
TabPageFocused = tabPageNext;
}
internal bool SelectionIsFresh()
{
if (TabPageFocused != tabPageLast)
return false;
tabPageLast = new TabPageEx();
return true;
}
internal class MyTabControlDesigner : ControlDesigner
{
void OnSelectionChanged(object sender, System.EventArgs e)
{
IDesignerHost dh = (IDesignerHost)GetService(typeof(IDesignerHost));
IComponentChangeService cs = (IComponentChangeService)GetService(typeof(IComponentChangeService));
DesignerTransaction dt;
if (!tabControl.SelectionIsFresh())
return;
dt = dh.CreateTransaction("Select MyTabPage");
cs.OnComponentChanging(tabControl, null);
tabControl.OnSelectionChanged();
cs.OnComponentChanged(tabControl, null, null, null);
dt.Commit();
}
I hope this helps anyone else in a similar situation.
Thank you for your assistance, Martin , the only person who cared during my plight .
Soog.
|
|
|
|
|
Well Done!
Happy it works for you now!
Got my '5' for sharing it with the community.
All the best,
Martin
|
|
|
|
|
Hello,
I currently have the problem that I want to update the contents (a string) of an item of a listbox with a textbox. Best would be on-the-fly so I tried listBox1.Items[listBox1.SelectedIndex] = textBox1.Text in TextChanged.
However I also want to be able to select which item to update by selecting the item in that very listbox. So I do something like textBox1.Text = listBox1.Items[listBox1.SelectedIndex] in SelectedIndexChanged of the listbox. (I say "something like" because the information includes other numbers that are also stored in a list along with the text.)
The problem with that is that selecting an item changes the text in the textbox and that again updates the item which implicitly calls an SelectedIndexChanged again (or so it seems). And during that updating of the listbox it happens that the selectedindex changes to -1. Which is a problem if you try to read that item...
I was wondering if there is an easier/better/cleaner way to accomplish what I try?
Stephan
|
|
|
|
|
Simple fix, just add if (listBox1.SelectedIndex > -1) so that the conditional is evaluated before making any updates. This should resolve your issue and update the reruired data. For example:
<br />
private void textBox1_TextChanged(object sender, EventArgs e)<br />
{<br />
if (listBox1.SelectedIndex > -1)<br />
{<br />
listBox1.Items[listBox1.SelectedIndex] = textBox1.Text;<br />
}<br />
}<br />
<br />
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)<br />
{<br />
if (listBox1.SelectedIndex > -1)<br />
{<br />
textBox1.Text = listBox1.Items[listBox1.SelectedIndex].ToString();<br />
}<br />
}<br />
-Arcond
|
|
|
|
|
Thanks for your answer, I found another solution myself today.
Stephan
|
|
|
|
|
Hello,
I am looking for a fast and efficient way to read the overall/average brightness of a 640x480 JPEG image. Quick background, I have a CCTV camera outside my house that I can control through ActiveX control in my application. I have code to grab a sample image every x minutes. I then want to read the overall brightness of this sample image to determine if it is dark, bright, etc... so I can adjust gain on the camera, etc... Later I can create a set of parameters to map brightness value ranges to approximate time of day and camera settings.
I understand from what I have read that GetPixel is very slow and that some unsafe code may be quicker but I am still new to image processing in C# so I don't fully understand that.
I have written this code and it works but as you can see, it returns to me (via the messagebox) the brightness of each pixel as it iterates through the loop. This is really annoying because because it kept popping up a messagebox for every pixel and there was no way I was going to sit there and hit "OK" for the whole 640X480 image - so i had to end task. I will pause so you can finish laughing at me, it was a stupid mistake.
public void LoadImage()
{
string strFileName;
strFileName = textBox1.Text;
Bitmap original_bitmap = new Bitmap(strFileName);
pictureBox1.Image = original_bitmap;
Color pixel;
float b;
for (int y = 0; y < pictureBox1.Image.Height; y++)
{
for (int x = 0; x < pictureBox1.Image.Width; x++)
{
//Get a pixel from the image
pixel = original_bitmap.GetPixel(x, y);
//read brightness of pixel
b = pixel.GetBrightness();
//convert float value of pixel to string
string result = b.ToString();
//show pixel brightness to user
MessageBox.Show(result);
}
}
}
At any rate, how do I calculate and output the overall image brightness and not just the brightness of each individual pixel? For now I would like to output to a messagebox for my benefit but later I can obviously store it in a variable to be used by my app. Is the way I am doing it going to be fast enough without having to learn the unsafe code method?
Thanks in advance!!!
Mike
|
|
|
|
|
newb2vb wrote: JPEG
The JPEG is irrelevant, it's decompressed to a bitmap in memory.
newb2vb wrote: I then want to read the overall brightness of this sample image to determine if it is dark, bright, etc...
You may want to convert your pixels to HLS, in order to work out brightness. Then you can check luminance.
newb2vb wrote: . I will pause so you can finish laughing at me, it was a stupid mistake.
Not at all, it was a viable way to test your code and see what the result was. I'd have used Console.WriteLine and checked the result coming out in the IDE, but either way, it's just test code.
newb2vb wrote: I understand from what I have read that GetPixel is very slow and that some unsafe code may be quicker but I am still new to image processing in C# so I don't fully understand that.
Yes, you should lock the bits and iterate over them.
My image processing articles will show you how to iterate over the bits of an image, and I have one on converting between RGB and HLS as well. To calculate overall brightness, I'd add the lumanance of each pixel and work out the average across the bitmap.
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,
Thank you for the answer, this helps alot. Just one more question if you don't mind.
The way the sample code I attached in my first message shows, the code will loop through x first from 0 to 640, then it will work y from 0 to 480. I understand what you suggested above to do a simple add and divide by to get the average. How do you recommend I buffer/add all 640 iterations on the first loop, x (then divide by 640) and then do the same for y? Or do I have the concept right but am approaching it all wrong?
Thanks,
Mike
|
|
|
|
|
I'd just keep a running total and then divide it. Your only issue is if the number of pixels means a high value would be bigger than the max of an unsigned int. Then you need to be more creative.
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 )
|
|
|
|