|
If you want to determine if the elements of your Array reference an object of a class, you have to iterate over your Array and test them against null, like Colin said.
Alternatively, you could use an ArrayList instead of the Array. The ArrayList class provides the Count property which tells how many objects really exist in your ArrayList.
www.troschuetz.de
|
|
|
|
|
Is it possible to force an event to trigger in C#?
eg. I would like to trigger the Button2_click event every time the Button1_Click event is triggered, so as to execute BOTH the code of Button1_Click and Button2_Click.
.::YF::.
|
|
|
|
|
Register one event handler for both of them.
Mazy
"One who dives deep gets the pearls,the burning desire for realization brings the goal nearer." - Babuji
|
|
|
|
|
Thanx! You really helped me out!
.::YF::.
|
|
|
|
|
Hi,
Let's face it, I don't understand how I can do the following. Let's explain a little :
* I have an online database (in fact mysql). I access this base through ByteFX's MySQLDataAdapter.
* I have an application which can read data from it but also can modify them WITHOUT necessary doing a fill (updating the database back). In fact, this application could be closed without having done the updates, inserts, etc.
* I do not have issues like online datas beeing changed or multi-people acceding the datas. Only this application is responsible of the database.
What I want is when the application is launched again, the "not updated yet" part of the datas are still there. So I need to cache them in some file.
What is the best approach to do that ?
Thanks
|
|
|
|
|
I use mousemove to draw a rectangle,when mousemove , it draw many rectangle,but i only want the latest rectangle,how to erase the previous rectangle.
It's the best to give a simple sample! thanks!
|
|
|
|
|
Before you you draw the rectangle, clear the graphics object.
dim g as graphics = mybase.creatgraphics
g.clear
g.drawrectangle(p,0,0, 15,15)
Aaron Eldreth
TheCollective4.com
My Articles
While much is too strange to be believed,
Nothing is too strange to have happened.
- T. Hardy
|
|
|
|
|
I have hit snag with my current project, I was hoping one of you guys and girls could help a newbie out.
As part of the process I call an external app, when called Windows gives a warning. This warning is not a critical one, but not one I can disable either.
What I would like to do is simply close that window from my code and let the external app get on and do it's job. What I am hoping is that someone can point me in the right direction as to how to close the window concerned as it lies outside my project.
Many thanks in advance,
Chris
|
|
|
|
|
To close a window, you need to call some low level Win32 APIs such as FindWindow, PostMessage, or EndDialog.
I don't know how to do this in C#, you can probably use unmanaged C++. The complication is, you may need to execute the code (the low level api calls) from the external program. If you do it from your own program, it may not work or it may generate "access denied" error.
In other words, you need to know how to build a dll, load it into an external process, and execute it. The whole thing is pretty complicated, here are some articles on CodeProject that might help you:
Three Ways to Inject Your Code into Another Process[^]
Sending windows messages from a remote machine[^]
Good luck.
My articles and software tools
|
|
|
|
|
You will have to P/Invoke FindWindow and SendMessage or PostMessage, sending the WM_CLOSE message, to the window handle that is returned by FindWindow.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
When i debug my project, it displays this message :
Here
And it auto break debug, what happen? please help me. Thanks
|
|
|
|
|
Which line of code does this happen? See the ArithmeticException class in MSDN too see what cause this in that line\.
Mazy
"One who dives deep gets the pearls,the burning desire for realization brings the goal nearer." - Babuji
|
|
|
|
|
Hi,
(Working on a windows Form)
I'm importing names from a db into a listbox. I want the user to be able to click on one of the names and have text boxes populated (from the db). In order to do this, I need to assign the id number (from the db) to each name in the listbox. Can you assign a hidden value to items listed in listbox?
thanks,
Ron
|
|
|
|
|
|
Thanks for the info Maysam!
I'm new to programming... it seems like a lot of code to
hide a variable with each listbox item. Is it because listbox
was not originally designed to have hidden variables?
Ron
|
|
|
|
|
I have a bitmap...
Bitmap bmp = new Bitmap("tiff48bppRgb.tiff")
The pixel format for the bitmap is...
System.Drawing.Imaging.PixelFormat.Format48bppRgb
My problem is that when I call bmp.GetPixel(0,0), I get a Color object that only represents 32bppArgb. So the extra information (48bppRgb) is lost.
Please Help! I have spent the last week programming an application, and I really need to extract 48bppRgb info from the Bitmap object, otherwise all is lost.
Dave
|
|
|
|
|
You can't use GetPixel because the Color it returns only supports 8 bits per channel.
You'll have to write your own code to return the colors and manipulate them on a per pixel basis. Start by reading this[^] series of excellent articles by Christian Graus. He demonstrates the technique your looking for.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Fantastic, this has really helped. Thanks very much!
As a side note, my TIFF files were originally recorded by a camera that is built for the scientific community. Unlike most commercial cameras, this camera does not automatically transform the amount of photons it collects on each pixel (this transformation is similar to a gamma correction). After I applied the solution you suggested, I found that my numbers were systematically off. I had to correct my numbers by the following transform...
[R G B] is what I get from Bitmap.GetPixel
[r g b] is what I got from the unsafe code that I executed.
R = 2.5 * r^(1/2.4) - 0.056
G = 2.5 * g^(1/2.4) - 0.056
B = 2.5 * b^(1/2.4) - 0.056
Applying this transform reduced by Mean-Squared-Error per pixel to about 10^-7.
It seems liike the tags inside of the TIFF file recorded the fact that a transform had not been applied, so GDI+ does the transform automatically when creating the Bitmap object. Add that to the list of things that GDI+ does without us knowing!
Dave
|
|
|
|
|
It does this probably because of the color management profile for the video device (monitor/video card). Go to the Display Properties Control Panel/Settings/Advanced/Color Management tab for a look at what I'm talking about.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Good idea, but I didn't find anything there. My machine's gamma / contrast / brightness settings don't predict what I'm seeing.
Apparently, the transform only happens when I use 48-bpp TIFF files from the special camera because it didn't happen when I used normal 24-bpp BMP files.
For the special camera's TIFF files, the GDI+ generates a decompressed pixel value equal to P, whereas the raw stored value is actually p.
gamma = 2.45
P = gamma * p^(1/gamma)
So it must be something with the TIFF decompressor in combination with a tag set by the camera. Thanks again for the help. Case closed.
Dave
|
|
|
|
|
I'm posting the C# solution to my original problem so that anybody who runs into the same problem can have the solution.
First, you need to import a very nice C++ DLL called BMGLib. Get it from here: http://members.cox.net/scottheiman/bmglib.htm (please see website for associated credits).
Next, create a Bitmap object (call it bmp) using UnsafeSupport.OpenBitmap. (NOTE: I am giving you the UnsafeSupport class at the end of this e-mail.)
To extract the color information from each pixel, execute the following steps. Given a pixel (int x, int y) and a Bitmap (bmp), you want a vector (float[3] rgb which contains values between 0 and 1 for each color).
float[] rgb = new float[3];
int[] xargbBppCum = UnsafeSupport.XargbBitsPerPixel(BmpOrig.PixelFormat);
for(int i=1; i<xargbBppCum.Length; i++)
xargbBppCum[i] += xargbBppCum[i-1]; //cumulative value
UnsafeSupport.GetRgb(ref rgb, xargbBppCum, x, y, bmp);
That's it. Here is the UnsafeSupport class. It encapsulates the BMGLib DLL. OH, and special thanks to Dave and the article he referenced earlier in the thread.
public unsafe class UnsafeSupport
{
/// <summary>
/// Enumeration supporting LibTiff (and ultimately Bmglib.dll).
/// </summary>
enum TiffCompressionEnum
{ NONE, CCITTRLE, CCITTFAX3, CCITTFAX4, LZW, JPEG6,
JPEG_DCT, NeXT, CCITTRLEW, MACINTOSH, THUNDERSCAN,
PIXARFILM, PIXARLOG, ZIP, KODAK, JBIG };
/// <summary>
/// Enumeration supporting LibTiff (and ultimately Bmglib.dll).
/// </summary>
enum TiffPhotometricEnum
{ MINISWHITE, MINISBLACK, RGB, PALETTE, MASK,
SEPARATED, YCBCR, CIELAB, CIE_LOGL, CIE_LOGLUV };
/// <summary>
/// Enumeration supporting LibTiff (and ultimately Bmglib.dll).
/// </summary>
enum TiffOrientationEnum { TOPLEFT, BOTTOMLEFT };
/// <summary>
/// Enumeration supporting LibTiff (and ultimately Bmglib.dll).
/// </summary>
public struct TIFFInfoStruct
{
TiffCompressionEnum compression;
TiffPhotometricEnum photometric;
TiffOrientationEnum orientation;
ushort predictor;
}
/// <summary>
/// The single import from Bmglib.dll. Supported formats and credits associated with the DLL are listed at
/// http://members.cox.net/scottheiman/bmglib.htm
/// </summary>
/// <param name="filename">string The full path of the bitmap file. The file extension determines the type of decompression.</param>
/// <param name="parameters">(void*)TIFFInfoStruct* Only returns information when a TIFF is decompressed.</param>
/// <param name="blend">int Alpha blending.</param>
/// <returns>IntPtr Handle to a GDI BITMAP object. Use Bitmap.FromHbitmap(IntPtr) to generate a Bitmap object.
/// The high-bit color information will be preserved.</returns>
[DllImport("Bmglib.dll", ExactSpelling=true, SetLastError=true)]
public static extern IntPtr CreateBitmapFromFile( [MarshalAs(UnmanagedType.LPStr)] string filename,
void* parameters, int blend );
/// <summary>
/// Because GDI+ loses 16-bit color information (only preserves 8-bit info) when opening a Bitmap object, this method
/// creates a Bitmap object by using GDI (thus skirting the GDI+ problem). After the GDI BITMAP object
/// is created, the GDI+ bitmap object is then created from the HBITMAP associated to the GDI BITMAP object. The end result
/// is that the 16-bit-color inforamtion is not lost. The endeavor is accomplished by calling the CreateBitmapFromFile method
/// in Bmglib.dll. The file extension determines the type of decompression. Supported formats and credits associated with
/// the DLL are listed here: http://members.cox.net/scottheiman/bmglib.htm
/// </summary>
public static Bitmap OpenBitmap(string filename)
{
TIFFInfoStruct tis = new TIFFInfoStruct();
IntPtr hb = CreateBitmapFromFile( filename, (void*) &tis, 0 );
return Bitmap.FromHbitmap( hb );
}
/// <summary>
/// Because GDI+ loses 16-bit color information (only preserves 8-bit info), this method searches the bitmap object and extracts
/// the appopriate 16-bit information about the RGB values for a pixel.
/// </summary>
/// <param name="RGB">float[2] Contains the output. Outputs the RGB values as values within the range of 0 to 1.</param>
/// <param name="xargbBppCum">int[5] 0, wasted bits per pixel; 1, bits per alpha pixel; 2, bits per red pixel; 3, bits per green pixel; 4, bits per blue pixel.</param>
/// <param name="x">int The x-coordinate of the pixel.</param>
/// <param name="y">int The y-coordinate of the pixel.</param>
/// <param name="bmp">Bitmap The bitmap object to extract the pixel information from.</param>
public static void GetRgb(ref float[] RGB, int[] xargbBppCum, int x, int y, Bitmap bmp)
{
RGB[0]=RGB[1]=RGB[2] = 0.0f;
BitmapData bd = bmp.LockBits( new Rectangle(x,y,1,1), ImageLockMode.ReadWrite, bmp.PixelFormat);
// bits per pixel = xargbBppCum[4]
int bypp = xargbBppCum[4]/8;
int jump = bypp*(x+bmp.Width*y);
int i;
ulong pixVal = 0ul;
ulong temp;
for(i=bypp-1; i>=0; i--)
{
pixVal = pixVal << 8;
temp = (ulong)* ( ((byte*)(void*)bd.Scan0) +i );
pixVal += (ulong)* ( ((byte*)(void*)bd.Scan0) +i );
}
int ctBit = xargbBppCum[4];
i=4;
ulong pval = 1ul;
while(ctBit >= 0 && i>1)
{
if(ctBit > xargbBppCum[i-1])
{
if(pixVal % 2ul == 1ul) RGB[i-2] += (float)pval;
pval = pval << 1;
pixVal = pixVal >> 1;
}
else
{
if(bmp.PixelFormat == PixelFormat.Format48bppRgb)
RGB[i-2] /= (float)(16384ul);
else
RGB[i-2] /= (float)(pval-1ul);
i--;
if(i>1)
{
if(pixVal % 2ul == 1ul) RGB[i-2] =1.0f;
pval = 2ul;
pixVal /= 2ul;
}
}
ctBit--;
if(bmp.PixelFormat == PixelFormat.Format48bppRgb)
{
RGB[0] *=2.0f;
RGB[1] *=2.0f;
RGB[2] *=2.0f;
}
}
bmp.UnlockBits(bd);
}
/// <summary>
/// Used for manually striding a Bitmap object (using pointers in unsafe mode). This has only been checked for
/// PixelFormat.Format24bppRgb and PixelFormat.Format48bppRgb. Use with caution for other formats.
/// ------
/// In order to use with GetRgb, you need to translate the output into a cumulative vector. For example, given a
/// bitmap object called bmp, do the following:
/// int[] xargbBppCum = XargbBitsPerPixel(bmp.PixelFormat);
// for(int i=1; i<xargbBppCum.Length; i++) xargbBppCum[i] += xargbBppCum[i-1]; //cumulative value
/// </summary>
/// <param name="pf">PixelFormat The pixel format.</param>
/// <returns>int[5] 0, wasted bits per pixel; 1, bits per alpha pixel; 2, bits per red pixel; 3, bits per green pixel;
/// 4, bits per blue pixel.</returns>
public static int[] XargbBitsPerPixel(PixelFormat pf)
{
int[] output = new int[5];
if(pf == PixelFormat.Format16bppArgb1555)
{
output[0] = 0;
output[1] = 1;
output[2] = 5;
output[3] = 5;
output[4] = 5;
}
else if(pf == PixelFormat.Format16bppRgb555)
{
output[0] = 1;
output[1] = 0;
output[2] = 5;
output[3] = 5;
output[4] = 5;
}
else if(pf == PixelFormat.Format16bppRgb565)
{
output[0] = 0;
output[1] = 0;
output[2] = 5;
output[3] = 6;
output[4] = 5;
}
else if(pf == PixelFormat.Format24bppRgb)
{
output[0] = 0;
output[1] = 0;
output[2] = 8;
output[3] = 8;
output[4] = 8;
}
else if(pf == PixelFormat.Format32bppArgb)
{
output[0] = 0;
output[1] = 8;
output[2] = 8;
output[3] = 8;
output[4] = 8;
}
else if(pf == PixelFormat.Format32bppPArgb)
{
output[0] = 0;
output[1] = 8;
output[2] = 8;
output[3] = 8;
output[4] = 8;
}
else if(pf == PixelFormat.Format32bppRgb)
{
output[0] = 8;
output[1] = 0;
output[2] = 8;
output[3] = 8;
output[4] = 8;
}
else if(pf == PixelFormat.Format48bppRgb)
{
output[0] = 16;
output[1] = 0;
output[2] = 16;
output[3] = 16;
output[4] = 16;
}
else if(pf == PixelFormat.Format64bppArgb)
{
output[0] = 0;
output[1] = 16;
output[2] = 16;
output[3] = 16;
output[4] = 16;
}
else if(pf == PixelFormat.Format64bppPArgb)
{
output[0] = 0;
output[1] = 16;
output[2] = 16;
output[3] = 16;
output[4] = 16;
}
return output;
}
}
Dave
|
|
|
|
|
By the way, this solution was only tested for PixelFormat.Format24bppRgb and PixelFormat.Format48bppRgb. Use with extreme caution!
Dave
|
|
|
|
|
I'm trying to make a ticker that gets its information from an rss news feed.
I can't figure out how to make the link portion be in sync with the title portion. What i mean is this.
Lets say i have titles
siteone
sitetwo
sitethree
I want it to scroll which i have it doing right now, but I want to be able to click on say sitetwo and it take me to toysrus.com or whatever and if i click on sitethree it take me to broadband.com or whatever the link in the rss file indicates. Here is what i have so far but obviously its not working. I can't figure out how to seperate if you will the links to correspond to the correct title. Never using the rss reader before so don't beat me up to bad if its an easy obvious fix.
void ticker()
{
String URLString = "http://www.local-news.net/go/-84.5374/33.8591/rss/";
XmlTextReader reader = new XmlTextReader (URLString);
string tickerstring = reader.ReadString();
while(true)
{
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.LocalName.Equals("title"))
{
tickerstring +=reader.ReadString()+" ";
}
if (reader.LocalName.Equals("link"))
link=reader.ReadString();
}
}
int removeme =-1;
for (int i=0; i<=tickerstring.Length; i++)
{
label2.Text=i.ToString();
if (i>100)
{
label1.Text = tickerstring.Substring(++removeme,label1.Text.Length);
}
else
try
{
label1.Text = tickerstring.Substring(0,i);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
Thread.Sleep(100);
}
Thread.Sleep(1000);
}
}
Win32newb
"Programming is like sex, make one mistake and you have to support it for a long time"
|
|
|
|
|
Update:
I have it reading it in and matching the links but I'm hung up on something that i thought would be obvious but its not. For some reason the link is one title behind. I see in the code that it reads the title then the link which is what it should do but as soon as it starts spitting out the title It should be linkable but the link hasn't changed. I have tried moving this segment of code all around and can't figure it out. Help would be appreciated.
if (reader.LocalName.Equals("link"))
link=reader.ReadString();
label2.Text=link.ToString();
void ticker()
{
String URLString = "http://rss.news.yahoo.com/rss/topstories";
string tickerstring="";
while(true)
{
XmlTextReader reader = new XmlTextReader (URLString);
int removeme =-1;
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.LocalName.Equals("link"))
link=reader.ReadString();
label2.Text=link.ToString();
Thread.Sleep(300);
if (reader.LocalName.Equals("title"))
{
tickerstring =reader.ReadString()+" ";
for (int i=0; i<=tickerstring.Length; i++)
{
if (i>100)
{
label1.Text = tickerstring.Substring(++removeme,label1.Text.Length);
}
else
{
try
{
label1.Text = tickerstring.Substring(0,i);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
Thread.Sleep(100);
}
}
}
}
}
}
Win32newb
"Programming is like sex, make one mistake and you have to support it for a long time"
|
|
|
|
|
hi all,
i can't access directly to the treeView.text property. my problem is how to clear the treeView content. i tryed also the resetText method but it doesn't resolve the problem.
|
|
|
|
|