|
And as I mentioned, make sure you're disposing all Bitmap (or Image ) and Graphics objects when done. Not disposing of them can cause memory problems, though the exact exception is ambiguous. Try my suggestions I posted before about using the using statement (or just code the try-finally blocks yourself; it's all compiled to the same IL) and see if that fixed anything.
Disposing of the Bitmap on which you're drawing will also make sure the file handle to which it's saved is disposed (at least it's supposed to; to be sure, write to a FileStream instead of using a filename String and dispose of it when finished saving).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I already tried your suggestions. I added using statement for all Graphics and Bitmap objects, but this didn't solve the problem.
I also commented out the line where the bitmap is saved to disk, to make sure this is no filehandle problem, but this also didn't help.
Is there a way to explicitly dispose all objects that aren't disposed after the first drawing?
Best Regards
Bernd R.
-------------------------------------------
The light at the end of the tunnel has been switched off temporarily due to budget problems...
|
|
|
|
|
Don't use it for the Bitmap you're going to return. If you do, the object will be disposed and anything you do with it will throw an ObjectDisposedException . Just dispose of everything else that implements IDisposable .
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I noticed that, when I tried to save the bitmap that was returned by the DrawChart method
I think I have to try out a memory profiler like this:
http://www.scitech.se/memprofiler/
Maybe this leads me to the problem.
-------------------------------------------
The light at the end of the tunnel has been switched off temporarily due to budget problems...
|
|
|
|
|
Brush blackBrush = Brushes.Black;
for(int i = 0; i < m_DataSource.Count; i++)
{
SolidBrush sliceBrush = new SolidBrush(m_DataSource[i].SliceColor);
graphics.FillRectangle(sliceBrush, origin.X + 5, origin.Y + 5 + (m_FontLegend.Height * i), 10, 10);
string strText = m_DataSource[i].Name +": "+ m_DataSource[i].Value.ToString();
int iStartX = origin.X + 20;
int iStartY = origin.Y + 1 + (m_FontLegend.Height * i);
graphics.DrawString(strText, m_FontLegend, blackBrush, iStartX, iStartY);
sliceBrush.Dispose();
}
blackBrush.Dispose();
See what the problem was? I disposed the system's black brush
-------------------------------------------
The light at the end of the tunnel has been switched off temporarily due to budget problems...
|
|
|
|
|
I've noticed that sometimes, specific parts of my application will run on my machine differently than the machine that it is actually intended to be run on. For instance, my computer, the one I am using to develop this application, there are MANY instances where the program has run flawlessly. However, when I move the application to the computers where it will be run, some things will either respond differently, or cause a crash. Examples of this include objects being disposed too early on the live-system machines, causing a crash. These same objects are alive and working fine at the same stages on the development machine. Another example is I want to simply position a button inside of a textbox (this is embedded in a datagrid column). Works fine on the development machine, and the button always shows up properly, and in the correct placement. However, on the live-system, the button seems to have a mind of its own, sometimes showing up, sometimes not, and usually always positioned in the wrong place.
So what is the deal with this type of behavior? Is this something that programmers will have to constantly deal with if using the .NET environment? The development and live-system computers all are running the same versions of the .NET Framework. I just don't get it. It is difficult to fix a problem when I can't duplicate the cause. Is this an inherent result of C#, or .NET, programs compiling as they are being run? I like the idea that they are actually being optimized for the specific machine they are being run on, but is the optimizer causing my programs to run differently on different machines? If that isn't causing the problem, then what in the world could it possibly be? I'm open to a lot of suggestions here!
Thanks, I hope somebody (Heath...hint hint) can help me out here.
|
|
|
|
|
First off, please don't address me personally. I'm flattered, but it's not very nice to others with good answers like Nick and Dave and others that show up from time to time.
Second, keep in mind that the .NET Framework - while the same on all Windows platforms (save the differences between Windows and Windows NT, like the FileSystemWatcher component that doesn't work on Windows), they rely on underlying system components like Windows Common Controls. Differences in the system there can make a little difference, though not as drastic as what you described with the positioned Button .
When it comes to positioning, the most common culprit is the DPI (dots per inch, sometimes refered to as PPI or pixels per inch). The difference in DPI affects how elements are positioned. As much as I hate VB (version 6 and below), it did get this right; it used twips, which is a logical unit (i.e., inches, points, centimeters, etc.). .NET uses pixels.
If you search the C# forum, you'll find several threads where I've posted more information about this including a snippet of code I use when necessary.
Other differences in behavior could be because you're using a debug build where another machine is using the release build. Debug builds are typically slower (not always true, but most often true). It could also be because you have some APIs (native functions, COM libraries, etc.) that you're P/Invoking or RCW'ing (it's a verb now, I say! ) that others don't have. Keep in mind when you deploy RCWs (interop assemblies), the corresponding COM libraries must be present as well (not necessarily the same version, but a version of the typelib and implementing COM server that uses the same CLSIDs and IIDs, assuming the COM server is written correctly).
For all these reasons, that's why companies test in large test bays with a multitude of different hardware and software configurations.
Also, before some *nix zealot jumps in and says "switch to linux", the same is true for linux and other *nix flavors as well. *nix solves DLL hell by using symbolic links but still requires that a library to which an executable was bound be installed on the platform.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thanks, I'll look into these causes and hopefully find the reasons.
I didn't mean to exclude anybody else, I just couldn't think of any other VIP names right offhand. I wanted a straightforward, and knowledgeable answer on this question, and I knew I'd get it if you answered. I appreciate the help!
- D
|
|
|
|
|
Hello people, I know that System.Security.Cryptography is a good way to protect data but now I have another problem. How to protect my private key? I can't leave may key on EXE or DLL's becaus C# is an easy way for reverse engeneering... I don't just said easy but... SO VERY MUCH EASY A LOT (lol)... What can I do to protect this?
Wender Oliveira
.NET Programmer
|
|
|
|
|
You wouldn't distribute your private key anyway. That's always bad no matter what you're doing.
If you're looking for a way to verify data, you'll want to use digital signatures. This allows you to distribute your public key (it's public, so it doesn't really matter*) and verify the signature of the signed data (it's a digest of the signed data encrypted with your private key; RSA and DSA implement this differently).
While my article probably isn't what you're looking for, read Using XML Digital Signatures for Application Licensing[^] for a good conceptual overview.
If you need to encrypt data, it's often best you use services provided by the OS. If you'll always run on Windows, read How To Create a DPAPI Library[^] on MSDN. This is a handy API for storing encrypted credentials, and is used by Passport (at least the local implementation that MSN and Windows Messengers use as well as IE for web sites). It's also very customizable and does most of the work for you.
If you want to implement your own encryption, generate a key pair on the machine after the app's installed. Store that in a key container. This method is fool proof, but not cryptoanalyst proof.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
i was under the impression that ANY time a control is accessed by a thread that didn't create the control, the call has to be marshaled to the creating thread, and .NET provides this by returning TRUE from Invoke.Required, and then using Invoke() passing it a delegate. many times i find myself forgetting to do this, and am reminded with mysterious crashes when the control is accessed from another thread. other times i forget, i am not reminded because it seems to work fine, which doesn't make sense to me. i stepped through code and confirmed that another (non-creating) thread was writing to a RichTextBox without a problem. my question is, why is this? is it just the richTExtBox that can be accessed from another thread?
|
|
|
|
|
You can call Control.Invoke, .BeginInvoke, .EndInvoke, .CreateGraphics on any thread you please.
The RTB will blow up on you eventually, though, I've seen it several times.
#include "witty_sig.h"
|
|
|
|
|
A flaw in .NET 1.x, which has been corrected in 2.0 beta so far. In fact you will get the following error image in Visual Studio 2005 when you try to perform such an act.
Threading Error[^]
- Nick Parker My Blog | My Articles
|
|
|
|
|
|
Heath Stewart wrote:
Been coding with threads improperly, have we Nick?
Nope, just testing things out to see the difference in 2.0, isn't that what every developer is doing??
Heath Stewart wrote:
I certainly hope they fix the escaped character problem, though!
I'm not aware of what your talking about, can you expand?
- Nick Parker My Blog | My Articles
|
|
|
|
|
Nick Parker wrote:
isn't that what every developer is doing??
No, I just compiled a simple app with one line: DoesEverything.TestAllNewAPIs() . Didn't you?
Nick Parker wrote:
I'm not aware of what your talking about, can you expand?
Expand! Excellent choice of words, and very relevent!
Look at the text in the screenshot. You'll see a lot of \r\n. They should be expanded (see what I mean) to the actual characters that they represent (0x10 and 0x13), not displayed in the text.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
"illegal operation occurred was:\r\n\r\n\tat"
#include "witty_sig.h"
|
|
|
|
|
Is it always a good idea to use Control.Invoke and Control.InvokeRequired ? Yes. Most definitely.
Is it always necessary? Not always.
Some things will work, but could possibly cause problems down the road. It's always a good idea to use the ISynchronizeInvoke implementation when you know that other threads will be updating controls, but you may not always see problems if you don't. This really comes down to the differences between PostMessage and SendMessage (native APIs) that are used internally (and some times you just get lucky).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi,
I'm facing the following problem in trying to code a DSP application in C#.
I need to create an array of objects of a class "cBiquad" using its default constructor.
The number of cBiquad objects is "mnNumBiquads".
How do I do this?
I tried the following:
int mnNumBiquads = (mnOrder + 1)/2;
int[] Biquad = new int[mnNumBiquads];
for (int i = 0; i < mnNumBiquads; i++)
{
Biquad[i] = new cBiquad();
}
However, this gives me the obvious error of not being able to implicitly convert cBiquad to int.
Could somebody suggest what I could do?
|
|
|
|
|
crushinghellhammer wrote:
Could somebody suggest what I could do?
int size = 10;
cBiquad[] cba = new cBiquad[size];
for(int i = 0; i < size; i++)
cba[i] = new cBiquad();
- Nick Parker My Blog | My Articles
|
|
|
|
|
Thanks, Nick. I *just* figured out myself, as well.
cBiquad[] arrBiquads = new cBiquad[mnNumBiquads];
for (int i = 0; i < mnNumBiquads; i++)
{
arrBiquads[i] = new cBiquad(); }
Is there a more elegant way of doing this, as in instantiating the array in just one line, using the default cBiquad constructor.
I mean:
cBiquad[] arrBiquads = something...
|
|
|
|
|
crushinghellhammer wrote:
Is there a more elegant way of doing this
Nope, that's about it. You could write a class factory of sorts that would take care of this for you, depending on what you are doing that may be overhead.
- Nick Parker My Blog | My Articles
|
|
|
|
|
Hi,
I'm trying to get a custom control to render its contents into a bitmap by sending it a WM_PRINT message. This works great if XP themes are not enabled. If XP themes are enabled, some controls (textbox, listview, listbox, treeview, datetimepicker) forget how to draw a themed border and draw an unthemed 3d border instead - and the groupbox only draws its background
Any ideas on how to get round this?
"I think I speak on behalf of everyone here when I say huh?" - Buffy
|
|
|
|
|
First, don't handle the WM_PAINT message. Override OnPaint for your controls (unless you're using an IMessageFilter or something) and use Control.SetStyle with the ControlStyles enumeration (read the .NET Framework SDK documentation for details) to control exactly what gets drawn.
If you don't want this code to run on Windows XP, use the Environment.Version property to determine on what platform and version of the OS the environment (the CLR) is running. Use that as a condition to executing certain code.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I have a number of controls that are interacted with using mouse events. These work fine until the area they are in is scrollable, then, because they are tied to the screen location rather than the parent client area location, they don't function properly.
How would I calculate the location in relation to the parent instead?
Seems like a simple thing, but i'm mildly confused by it.
Regards
Cata
|
|
|
|
|