|
string fileName = @"C:\myImage.jpg";
using (StreamReader fileReader = new StreamReader(fileName))
using (Stream Img = fileReader.BaseStream)
{
this.pictureBox1.Image = new System.Drawing.Bitmap(Img);
}
untested/use at own risk/not liable/blah blah...
-Spacix
All your skynet questions[ ^] belong to solved
I dislike the black-and-white voting system on questions/answers.
|
|
|
|
|
By any chance are you using Image.FromFile to load the image? If so, the file is locked until you dispose the image. You need to either, dispose of the image before you delete it or, use an alternative method for loading the image such as FromStream which does not lock the method.
|
|
|
|
|
hi guy, I am loading image in the picturebox control like this on form load event. but Still the file is gettting locked. I don't know where the problem is getting. Any help Thanks.
string sourceFile =imagefilepath;
using( Stream stream = File.Open( sourceFile, FileMode.Open,FileAccess.Read, FileShare.Read ) )
{
try
{
Image image = Image.FromStream( stream );
stream.Close();
stream.Dispose();
pictureBox1.Image = image;
}
catch( Exception ex )
{
MessageBox.Show("so ");
}
}
|
|
|
|
|
That code should not give you a sharing violation when trying to delete the file further on. If it does, first make sure nobody else is actually using the file or keeping it open (perhaps not your application at all - or another part of it). Try using filemon (google that) to see what happens.
Some thoughts, however:
1. Make sure the picture box isn't still holding a reference to your image when you dispose of it. Otherwise, you will crash (either with an exception or with strangeness - this will depend on the current mood of your GDI). Always set pictureBox1.Image to null before you call image.Dispose() .
2. You are calling stream.Dispose() even though you're in a using block for stream . Don't. The using block does that for you when you exit it - regardless of exceptions or whatever else may happen. That's the reason using blocks exist. Also note that stream will close itself when it gets disposed (unless you do it first), so the stream.Close() call can be dropped as well.
Later,
--
Peter
|
|
|
|
|
Hy there,my problem is this: I have an image in my project resources,and a control that has an Image property set to that image. At runtime,I want to know what image is in that property. So i say
if(image == Properties.Resources.img1)
do something
I know that that is the image in the property,so why isn't it working?
|
|
|
|
|
System.Drawing implements neither the == operator nor the Equals() Method. So therefore it inherits the Equals() implementation from Object which checks object references. And so it fails. Using the == operator on an image is rather absurd anyhow. If it's an issue, you'll have to implement your own Equals or == routines and check the pixels one at a time, or use the object references to your advantage.
Scott P
“It is practically impossible to teach good programming to students that have had a prior exposure to BASIC: as potential programmers they are mentally mutilated beyond hope of regeneration.”
-Edsger Dijkstra
|
|
|
|
|
Any idea how to convert strings like "Tuesday 1st April 2008" to a DateTime.
I have tried DateTime.TryParse with different CultureInfo, without result
Kjetil
|
|
|
|
|
|
Thanks
Useful tip, but I was not able parse the date
DateTime dt = DateTime.ParseExact("April 1st 2008", "MMMM dd yyyy", new CultureInfo("en-US"), DateTimeStyles.None); //Failed
DateTime dt = DateTime.ParseExact("April 1 2008", "MMMM dd yyyy", new CultureInfo("en-US"), DateTimeStyles.None); //Failed
DateTime dt = DateTime.ParseExact("April 01 2008", "MMMM dd yyyy", new CultureInfo("en-US"), DateTimeStyles.None); //Succeeded
Any idea what formatstring to use ?
I could manipulate the string before parsing, but I'm parsing 8 different languages with different date format :-/
Kjetil
|
|
|
|
|
I don't think it's possible to have one generic format for the above examples.
Either some manipulation and FormatException handling is going to be required, or you're going to have to force them in your UI to enter data in an acceptable format by either separating the day, month and year fields into separate controls or using a DateTimePicker or something similar.
The second two examples shouldn't be too hard, but the date suffix in the first does not appear to be included in any of the custom format mechanisms - unless it's possible to use a wild card for the two characters?
It may be easier to create your own control that can handle the 8 different language formats - that will give you more flexibility if more are required later.
Dave
|
|
|
|
|
Guess I have to do some string-manipulation.
My app reads the dates from different sources, like word, excel, text or xml files, so it's not possible to solve this by UI changes...
Thanks
Kjetil
|
|
|
|
|
Hi All,
i have to show the starttime and the endtime of a activity in a progressbar. the bar has to peform one step every minute.
Can someone explain to me how to that with some code?
Thanx.
|
|
|
|
|
|
I have a problem while trying to execute a delegate in a different thread.
I have my application and autocad that has loaded my .net-dll.
I created a named-pipe-server (in an own thread in the dll for autocad) and send messages to it from my application. The thread with the server watis for the messages and executes a delegate on receiption. The method in my class (in the dll) is called and I can show it with a messagebox.
Till here everything is fine.
But now I want to open a new file for autocad and I try to use the regarding method-call (AcadApp.Application.DocumentManager.Open(file)). But here is the problem. I get an exception about internal error or wrong context. I'm still in the thread-context of my listening server and I think that is the problem. But how can I execute a function in the autocad-thread? My initialization-function that creates the listening-thread is executed in the autocad-thread-context so I can get Thread-object or whatever is needed.
|
|
|
|
|
You can try using MethodInvoker inside your thread to invloke the function in another thread.
Thanks
Laddie
Kindly rate if the answer was helpful
|
|
|
|
|
How does it work? Can you give me a sample? As you see I tried something but it doesn't work.
my code:
class IPCMsgHandler
{
private Thread listenThread = new Thread(ListeningThreadFunc);
...
public void Start()
{
listenThread.Start(this);
}
private static void ListeningThreadFunc(object data)
{
IPCMsgHandler helper = data as IPCMsgHandler;
...
MethodInvoker mi = new MethodInvoker(helper.OnMessage);
mi.Invoke();
...
}
}
public class AutoCADApplication : IExtensionApplication
{
IPCMsgHandler helper = null;
public void Initialize()
{
this.helper = new IPCMsgHandler("AutoCAD-IPCServer", new IPCMsgHandler.MessageHandler(this.OnIPCMessage));
this.helper.Start();
}
private void OnIPCMessage()
{
return;
}
|
|
|
|
|
Hi,
You need a reference to a control that has a window handle associated with it (something that inherits from System.Windows.Forms.Control) and lives on the Autocad main thread. Possibly you can get that through the Autocad API (with which I'm not at all familiar).
If you have that reference in a variable called control you invoke your delegate with control.Invoke(mi) instead of mi.Invoke() .
This routes the invocation through the event queue on the UI thread, which should solve your problem. Assuming you can get your hands on a suitable Control object of course...
--
Peter
|
|
|
|
|
That would work for a windows forms based API, and although I don't know the Autocad API either I wouldn't expect it to be Forms based. A better bet might be to implement a producer/consumer type of architecture[^].
Standards are great! Everybody should have one!
|
|
|
|
|
True... but the API might contain something similar. I'd look around for a method called Invoke() on objects exposed by it. That mechanism isn't specific to Windows.Forms.Control , but to the System.ComponentModel.ISynchronizeInvoke interface, which is implemented by controls. Chances are that there is something similar available (there should be, if cross-thread calls are an issue).
However... I don't seem to remember the "wrong execution context" error message from inadvertently doing cross-thread method invocations in Windows.Forms. This could be something completely different. If it has anything to do with a System.Threading.ExecutionContext , there is something seriously broken in AutoCAD or its API. In the code presented, the OnIPCMessage() method shouldn't care less about from which thread it is invoked, and even if it did, the ExecutionContext should be just fine (it gets copied when the managed thread is started).
Or could it be that the code causing the problem isn't included in the post?
Nite!
--
Peter
|
|
|
|
|
How can you read USB device names and know the according com-port?
Thanks,
Kristof
|
|
|
|
|
|
I've been playing around with the StringBuilder class, and found something confusing...
The code is as follows:
static void Main(string[] args)
{
Stopwatch sw1 = new Stopwatch();
Console.WriteLine("Press enter to being the test.");
Console.ReadLine();
Console.WriteLine("Test 1 is addind 10 characters 1,000,000 times.");
Console.WriteLine("Test 1 Starting");
string smallWord = "1234567890";
StringBuilder longWord = new StringBuilder();
sw1.Start();
for (int j = 0; j < 1000000; j++)
{
longWord.Append(smallWord);
}
sw1.Stop();
Console.WriteLine("Test 1 Complete in " + sw1.ElapsedMilliseconds + "ms");
Console.WriteLine("-");
Console.WriteLine("Test 2 is removing 10 characters 500 times.");
Console.WriteLine("Test 2 Starting (Will take several seconds)");
sw1.Reset();
sw1.Start();
for (int k = 0; k < 500; k++)
{
longWord.Remove(0, 10);
}
sw1.Stop();
Console.WriteLine("Test 2 Complete in " + sw1.ElapsedMilliseconds + "ms");
Console.WriteLine("-");
Console.WriteLine("Press Enter to Quit.");
Console.ReadLine();
}
My question:
How come it is EXTREMELY fast(91ms) to ADD 10,000,000 characters, yet takes SO long (+- 6 seconds) to remove a total of 5000 characters?
Is there anyway to optimise the above code to make it any faster?
Any help would be appreciated
-= Reelix =-
|
|
|
|
|
The StringBuilder uses an internal buffer that normally has some unused space at the end, so when you add characters to it, they are just placed in this unused space. It's only when it runs out of unused space that it has to allocate a new buffer an copy all data to it. The new buffer is twice the size of the original buffer, so it only has to do this 24 times to accomodate 10000000 characters.
If you remove some characters at the beginning of the buffer, all the remaining characters have to be moved. Doing this 500 times should take somewhere around 100 times as long as creating the string. Which is does.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
sounds interesting. another point is, that a test 3 (removing backwards) will run in 0ms (?!):
sw1.Start();
for (int k = 0; k < 500; k++)
{
longWord.Remove(longWord.Length - 10, 10);
}
sw1.Stop();
|
|
|
|
|
buchstaben wrote: another point is, that a test 3 (removing backwards) will run in 0ms (?!):
That is because the internal length of the string is being updated only. The memory is not being released. Therefore all you are doing is updating an int 500 times - That is a very cheap operation.
|
|
|
|