|
|
do apologise i should have tried your code first it worked a treat.
|
|
|
|
|
how can i create a new bitmap file?
i can not find the constructor,all the constructor is for the existing file?!
|
|
|
|
|
Bitmap b = new Bitmap ( width, heoght );
b.Save ( filename );
Q:What does the derived class in C# tell to it's parent?
A:All your base are belong to us!
|
|
|
|
|
Thank you very much
i think i got it
|
|
|
|
|
And if you want to paint on it:
Bitmap bmp = new Bitmap(width, height);
Graphics g = Graphics.FromImage(bmp);
g.FillRectangle(Brushes.Red, new Rectangle(0, 0, bmp.Width, bmp.Height));
g.Dispose();
bmp.Save("C:\\Image.png", ImageFormat.Png);
bmp.Dispose();
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Ive been programming in C# for a while now, but there are a few technical performance issues I don't know yet.
There are a couple of things I use a lot (and I am growing used to using them), like ArrayLists.
I want to know which of these really easy to use things, like foreach loops, ArrayLists etc has a big performace hit.
And if they do, what should I use when performance is a great issue?
|
|
|
|
|
I myself, always try to use for loop which is much faster than foreach loop . On the other hand, if I have a class named Class1 , I'd prefer to store it in an Array of its own type instead of ArrayList , getting rid of type casting issues.
Don't forget, that's Persian Gulf not Arabian gulf!
Murphy: Click Here![^] I'm thirsty like sun, more landless than wind...
|
|
|
|
|
Type-casting isn't so much of an issue with reference types. It results in only 1 to 2 extra IL instructions. When casting value types, however, the value type must be unboxed (it's boxed in the ArrayList since it stores object s) and that is a serious performance hit. That's when a typed array (like int[] ) is definitely better.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Boxing is very expensive, and I always avoid it, but even casting can introduce more performance drain than you may think. Try running this code on your setup to see what I mean.
<br />
long startTime, endTime;<br />
int loopCount = 10000000;<br />
<br />
Guid guid = Guid.NewGuid();<br />
Guid guid2 = guid;<br />
object o = guid;<br />
object[] objectArray = {guid};<br />
Guid[] guidArray = {guid};<br />
<br />
startTime = DateTime.Now.Ticks;<br />
for(int x = 0; x < loopCount; x++) {<br />
guid = (Guid)objectArray[0];<br />
guid = (Guid)objectArray[0];<br />
guid = (Guid)objectArray[0];<br />
}<br />
endTime = DateTime.Now.Ticks;<br />
Console.WriteLine(((endTime - startTime) / 10) + " microseconds total (accessing array of objects)");<br />
<br />
startTime = DateTime.Now.Ticks;<br />
for(int x = 0; x < loopCount; x++) {<br />
guid = guidArray[0];<br />
guid = guidArray[0];<br />
guid = guidArray[0];<br />
}<br />
endTime = DateTime.Now.Ticks;<br />
Console.WriteLine(((endTime - startTime) / 10) + " microseconds total (accessing array of Guids)");<br />
<br />
startTime = DateTime.Now.Ticks;<br />
for(int x = 0; x < loopCount; x++) {<br />
guid = (Guid)o;<br />
guid = (Guid)o;<br />
guid = (Guid)o;<br />
}<br />
endTime = DateTime.Now.Ticks;<br />
Console.WriteLine(((endTime - startTime) / 10) + " microseconds total (assigning Guid from object w/ cast)");<br />
<br />
startTime = DateTime.Now.Ticks;<br />
for(int x = 0; x < loopCount; x++) {<br />
guid = guid2;<br />
guid = guid2;<br />
guid = guid2;<br />
}<br />
endTime = DateTime.Now.Ticks;<br />
Console.WriteLine(((endTime - startTime) / 10) + " microseconds total (assigning Guid from Guid)");<br />
<br />
Environment.Exit(0);<br />
<br />
This gave the following output on my setup, a dual P4 Xeon workstation running VS.NET 2003 in Debug mode:
468750 microseconds total (accessing array of objects)
421875 microseconds total (accessing array of Guids)
484375 microseconds total (assigning Guid from object w/ cast)
375000 microseconds total (assigning Guid from Guid)
Casting overhead is actually one of the major performance drains when using generic collections, after things like method-call overhead and synchronization. I'm not saying that nobody should use collections, but it's good to know how you're spending cycles. I agree with maysam; there's no reason to cast unnecessarily, and good reasons to avoid it.
Regards,
Jeff Varszegi
EEEP!
|
|
|
|
|
A Guid is a value type, so there is boxing/unboxing involed with casting.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Sheesh, you're absolutely right. Thanks-- I thought those numbers looked suspiciously close, but I chalked it up to running in Debug mode, and I never realized that Guid is a struct! I ran this code:
<br />
long startTime, endTime;<br />
int loopCount = 10000000;<br />
<br />
string s = "";<br />
string s2 = s;<br />
object o = s;<br />
object[] objectArray = {s};<br />
string[] stringArray = {s};<br />
<br />
startTime = DateTime.Now.Ticks;<br />
for(int x = 0; x < loopCount; x++) {<br />
s = (string)objectArray[0];<br />
s = (string)objectArray[0];<br />
s = (string)objectArray[0];<br />
}<br />
endTime = DateTime.Now.Ticks;<br />
Console.WriteLine(((endTime - startTime) / 10) + " microseconds total (accessing array of objects)");<br />
<br />
startTime = DateTime.Now.Ticks;<br />
for(int x = 0; x < loopCount; x++) {<br />
s = stringArray[0];<br />
s = stringArray[0];<br />
s = stringArray[0];<br />
}<br />
endTime = DateTime.Now.Ticks;<br />
Console.WriteLine(((endTime - startTime) / 10) + " microseconds total (accessing array of strings)");<br />
<br />
startTime = DateTime.Now.Ticks;<br />
for(int x = 0; x < loopCount; x++) {<br />
s = (string)o;<br />
s = (string)o;<br />
s = (string)o;<br />
}<br />
endTime = DateTime.Now.Ticks;<br />
Console.WriteLine(((endTime - startTime) / 10) + " microseconds total (assigning string from object w/ cast)");<br />
<br />
startTime = DateTime.Now.Ticks;<br />
for(int x = 0; x < loopCount; x++) {<br />
s = s2;<br />
s = s2;<br />
s = s2;<br />
}<br />
endTime = DateTime.Now.Ticks;<br />
Console.WriteLine(((endTime - startTime) / 10) + " microseconds total (assigning string from string)"); <br />
<br />
<br />
Environment.Exit(0);<br />
and got this output:
234375 microseconds total (accessing array of objects)
62500 microseconds total (accessing array of strings)
187500 microseconds total (assigning string from object w/ cast)
46875 microseconds total (assigning string from string)
Casting introduces a big performance whack in Java programs, a little less in .NET ones in my experience, but still very significant.
Regards,
Jeff Varszegi
EEEP!
|
|
|
|
|
Excuse me,I don't know which message board is adapted to ask my question.
I want the stroke I painted in my C# project can be saved and used in PowerPoint 2003,
but TabletPC SDK's Ink.Save can only save ink for *.ISF or bitmap,
*.ISF can't be inserted into PowerPoint 2003,
I only find the *.emf can be used...
If I save my ink for bitmap,
because the background of the ink is not transparent,
When I insert it into PowerPoint 2003,
it would cover the original image in PowerPoint 2003...
Can I save my ink as *.emf or a transparent bitmap...??
|
|
|
|
|
EMF is a vector format, so it really isn't transparent or solid. It's a collection of strokes and fills, so if an area isn't filled it appears transparent.
You can insert a bitmap into PowerPoint, though. PowerPoint supports a wide variety of image formats.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I have a question about deployment of a C# application. I see in the MSDN how to create a shortcut to the desktop, but for the life of me, I cannot see how to setup the Start menu. Can anyone please point me into the right direction?
Thanks
Larry J. Siddens
|
|
|
|
|
If I am not wrong in understanding what your question is.
Why not create a setup kit for your application. You could write to the registry. You could create a program group in the start Menu.
|
|
|
|
|
You don't write to the registry to correctly create a program group.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Include a Windows Installer project in VS.NET. It's not very robust, but can get the job done. Add the outputs of your other projects in your solution (or the assemblies if working in a different solution, though the former way establishes build dependencies which can be desireable). Add the assemblies to the right folders (or the GAC) in the File System editor, then right-click and create a shortcut to your apps (and any other files, like a help file for example). Drag that shortcut to the Desktop folder or to a new Programs group (right-click on the top node to show additional special folders, such as the Programs folder).
The Windows Installer is a great, transacted installation runtime with many capabilities, though you won't find UIs for all that in the VS.NET Windows Installer project (it's meant to be basic, not commercial-grade for larger applications).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Dear Sirs,
I am locked.
Yes, I am about to finish multithreaded app. There are some tasks that need to perform some intense calculations and do require a lock in order to have all them performed.
But, from time to time other higher priority tasks demand the lock on those resources... How could I "manage" to force the lower priority task to smartly leave the lock in order to grant it to higher priority task?
I've tried Aborting lower priority with some state info object that helps me filtering it from real Abort situations.
Also, I would like to limit the amount of seconds that the higher priority is waiting for the lock before trying to force the lower priority task to forget about the lock.
Thank you's for all of you.
PS:I would like to avoid polling..
|
|
|
|
|
I would like to limit the amount of seconds that the higher priority is waiting for the lock before trying to force the lower priority task to forget about the lock
using System.Threading;
bool isLockAcquired = Monitor.TryEnter(yourLockedObject, maximumMillisecondsToWait);
if (!isLockAcquired) {
}
You have to be careful to release the lock if it's successfully acquired, but only then. The Microsoft .NET implementation will silently ignore requests to release an unheld lock, but Mono chokes on this; in this case, the Microsoft implementation is actually against the specified behavior! You should want to write code that's correct, anyhow. Be careful whenever you use Monitor.Enter() and Monitor.Exit() ; you can introduce subtle bugs that are easily avoided by using lock (obj) {} .
As for the polling, there's no help for it; it's better than forcing an abortion. Why the desire for avoidance? It's not expensive in the grand scheme of things to check a variable once in a while.
Regards,
Jeff Varszegi
EEEP!
|
|
|
|
|
A good way to do this is to use a try-finally block like so:
bool acquired = Monitor.TryEnter(yourLockedObject, maximumMillisecondsToWait);
try
{
if (acquired)
{
}
}
finally
{
if (acquired) Monitor.Exit(yourLockedObject);
} This is similar to what the lock statement does (i.e., compiles as), only it uses Monitor.Enter which no maximum wait time.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
You can force a thread to temporarily exit its synchronization domain by calling Monitor.Wait and specifying true for the exitContext parameter. The call will block until Monitor.Pulse or Monitor.PulseAll is called from another thread. It will then reenter the synchronization domain. This will increase the complexity of your code dramatically. If at all possible I always try to design an app so that it doesn't require this type of complex synchronization.
|
|
|
|
|
I have a couple of crystal reports in a C# app that will be exported excel by the client. While there is almost always data for every column/field of the reports, there are occasions where a particular field in a record is blank/null.
In the Crystal report viewer, everything looks great - even the blank data is lined up in the appropriate columns, etc. However, when I export to excel, any field that did not have data seems to disappear and then all the columns to the right of it shift over.
Kind of messy...and very misleading because data is ending up in the wrong columns in the spreadsheet.
How do I solve this? I tried playing around with the "convert NULL values to default" but didn't get anywhere. Is there a way to just specify a default value of a blank string or something so some value is present and nothing shifts?
Thanks!
|
|
|
|
|
Just use a function (either CR's scripting language or, IIRC, VBScript) that uses the data for that field and basically performs the ol' IIF-type functionality, where you either display the data if not null, or "display" something else (like " ").
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I want to define an arry of instance of ArrayList.
For example
ArrayList MyArrayList1 = new ArrayList();
ArrayList MyArrayList2 = new ArrayList();
ArrayList MyArrayList3 = new ArrayList();
ArrayList MyArrayList4 = new ArrayList();
In deed ,I hope MyArrayList1,MyArrayList2,MyArrayList3,MyArrayList4 are in a Array. Could realize it?
Thank you very much.
vigorous
|
|
|
|