|
Might as well give the "event" version that passes the string as an event arg...
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
form2.Clicked += new Form2.StringEventHandler(form2_Clicked);
}
private void form2_Clicked(string message)
{
textBox1.Text = message;
}
private void button1_Click(object sender, EventArgs e)
{
form2.Show();
}
private Form2 form2 = new Form2();
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (Clicked != null) Clicked("Here is some text");
}
public event StringEventHandler Clicked;
public delegate void StringEventHandler(string message);
}
|
|
|
|
|
Ok, the question is: how to load image from resource (and then do some manipulations on it)?
In a C++ app, i am simply doing:
specifying image in file myapp.rc like:
IDR_MYPIC RCDATA "Res\\bkg.jpg"
then in file resource.h:
#define IDR_MYPIC 111
then in application i can simply access it by:
HRSRC hResInfo = FindResourceW((HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
MAKEINTRESOURCEW(IDR_MYPIC), RT_RCDATA);
I am trying to do actually the same thing in C#, but after hours of searching, experimenting - i got nothing.
This code returns resources path:
System.Reflection.Assembly thisExe;
thisExe = System.Reflection.Assembly.GetExecutingAssembly();
string[] resources = thisExe.GetManifestResourceNames();
string list = "";
foreach (string resource in resources)list += resource + "\r\n";
MessageBox.Show(list);
which are:
ComboTest.Form1.resources
ComboTest.Properties.Resources.resources
Then i am trying do apply each path like this:
System.Reflection.Assembly thisExe;
thisExe = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.Stream file = thisExe.GetManifestResourceStream("ComboTest.Form1.resources.zzz.jpg");
this.pictureBox1.Image = Image.FromStream(file);
file always = null
Same thing with that:
IntPtr hModule = ResImage.LoadLibraryEx(Application.ExecutablePath,
IntPtr.Zero, 0x2 );
if (hModule == IntPtr.Zero) throw new Exception();
IntPtr hResource = ResImage.FindResource(hModule, "zzz.jpg", "JPEG");
hResource is always null.
My image file is "zzz.jpg" and i have imported it into a project by clicking on a main form (in a designer), clicking on BackgroundImage in Properties tab, then selecting "Project resource file" radio button and clicking "Import" button.
Image now appears in "Resources" folder in Solution Explorer tab.
So, how can i access it from code?
thanks
011011010110000101100011011010000110100101101110
0110010101110011
|
|
|
|
|
To access Image from Resource you can use
this.pictureBox1.Image = WindowsApplication1.Properties.Resources.zzz;
modified on Wednesday, January 5, 2011 7:57 AM
|
|
|
|
|
Hi,
I've just tried the following code that works for me:
ResourceManager resourceManager = new ResourceManager("WindowsFormsApplication1.Properties.Resources", this.GetType().Assembly);
Bitmap image = (Bitmap)resourceManager.GetObject("snapshot1");
image.Save("C:\\Temp\\Test123.bmp");
This code works if you add a bitmap called "snapshot.bmp" to your resources. Please make sure that the assembly string does match you project. You will find both strings in the "Resources.Designer.cs" file.
Have a look here for a more detailed explanation:
http://www.mysuperiorgames.com/resources.html[^]
|
|
|
|
|
Thanks guys, that clarified a few things.
Most important thing here for me is to make this work:
IntPtr hResource = ResImage.FindResource(hModule, "zzz.jpg", "JPEG");
I am trying to implement custom background image drawing from memory (cached image) in C# so the app will not flicker when its 2000 X 1000 px bkg image plus 100 of controls on a form. The only method i know is the only by using win32 API (already implemented in c++ project) but here i need a valid "hResource" parameter to pass to my native dll to do the job.
IS it possible to achieve? Maybe you know some ways? thanks in advance
011011010110000101100011011010000110100101101110
0110010101110011
|
|
|
|
|
Hi all. When i try accessing drive C: where the O.S.(windows 7), i keep on getting the error:
Access to the path 'C:\Documents and Settings\' is denied. and throws the Exception:
I have a good idea i have the following piece of code
private static void CopyFiles(string directoryPath, string destination)
{
try
{
int count = 0;
DirectoryInfo dir = new DirectoryInfo(directoryPath);
FileInfo[] wordFiles = dir.GetFiles("*.doc*", SearchOption.AllDirectories);
foreach (FileInfo f in wordFiles)
{
count++;
f.CopyTo(destination + count.ToString() + f.Name);
Console.WriteLine("***************************");
Console.WriteLine("File name: {0} Copied", f.Name);
Console.WriteLine("***************************\n");
}
}
catch (UnauthorizedAccessException ex)
{
}
}
I tried to request permisions in the two commented lines but i was totally unable. Please assist. .
Wamuti: Any man can be an island, but islands to need water around them!
Edmund Burke: No one could make a greater mistake than he who did nothing because he could do only a little.
|
|
|
|
|
Wamuti wrote: Access to the path 'C:\Documents and Settings\' is denied. and throws the Exception:
yes it will throws Exception because Microsoft replaced "Documents and Settings" folder with "Users" folder from Vista. hence same is the case in Windows 7 then if you want to access the "Documents and Settings" you should looking for Users folder
use given Path instead of C:\Document and Setting in Windows 7 or even in Vista
C:\Users
to get more clarification please read the thread of given link
C:\Document and Setting[Win 7][^]
modified on Wednesday, January 5, 2011 6:00 AM
|
|
|
|
|
Hi,
You should never have a hard coded folder name in your app.
C:\Documents and Settings\ might not exist for one of many reasons:
- your system drive may not be called C:
- your Windows system might be an international one using a different language
- your Windows system might be more recent than XP and use a completely different folder name.
The correct way is by using Environment.GetFolderPath() with the right parameter. That is bound to work on a range of Windows versions starting before XP. See the MSDN doc.
|
|
|
|
|
I understand. Supposing i wanted to write a file to a drive that is somewhat protected by the UAC, say Windows. This would throw an exception with an access denied message. I have a feeling i should request for permission, then the UAC would pop up, then if would say allow. Is that possible in code. That is really what i want to do. My English is not that good
Wamuti: Any man can be an island, but islands to need water around them!
Edmund Burke: No one could make a greater mistake than he who did nothing because he could do only a little.
|
|
|
|
|
yes, since Vista, some folders (not drives) are protected. The idea is your app should not write there at all since these folders have special functionality for all users, not just for you.
You can invoke the UAC dialog and get admin access rights if you must; see the relevant CodeProject articles[^].
|
|
|
|
|
LPXYZ
word cont*aining an asterisk
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
LPXYZ
word cont3aining a digit
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
LPXYZ
words containing a period
for.each
object.field
object.method(parameter)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
modified on Thursday, January 13, 2011 3:46 PM
|
|
|
|
|
LPXYZ double
words containing a period
for.each
object.field
object.method(parameter)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Hi developers
I want make a quiz program.
It use image recognition (recon the person from the given image) also.Every questions have a four options as answers.And the entire quiz is controlled with timer.Means it must end within the given time.Am really doubtful in so many factors.I searched many in CodeProject,but couldn't find an appropriate application to study.If you have any idea please help me.
I need help to build it in c#.net
varun
|
|
|
|
|
Hi,
the trickiest part will be the face detection. Here are two articles that describe some basics:
Face Detection in C#[^]
Face and Eyes Detection Using OpenCV[^]
You would need to capture an image with a webcam and try to recognize a face that is stored somewhere (image file or just in database). The whole quiz-timer thing should be quite simple. Try to break down your problem in simple tasks and solve them step for step.
|
|
|
|
|
Thanks first
Your help is so much valuable for me.but still i am confused in making that to work with in the given time.Can you point me to any c# source codes available
|
|
|
|
|
There is no ready to use source code that will do your task. This is why you need to develop it on your own. Try to learn about face recognition and the use of timers to control your quiz flow.
|
|
|
|
|
Thanks again
I will try to do that,and update it here
|
|
|
|
|
In the windows service, here is how I have setup the backgroundworker.
Question.
I do not know why the backgroundworker_progresschanged event does not get called.
Do you see what the problem is please?
Thanks
private BackgroundWorker backgroundWorker;
protected override void OnStart(string[] args)
{
backgroundWorker = new BackgroundWorker();
backgroundWorker.WorkerSupportsCancellation = true;
backgroundWorker.WorkerReportsProgress = true;
backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);
backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker_ProgressChanged);
backgroundWorker.RunWorkerAsync();
}
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
}
private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
}
|
|
|
|
|
|
I don't want to seem as though I'm being funny here, but how would it report progress. You have no functionality in your DoWork to report progress from. In your DoWork, you'd need to report progress using something like the following:
BackgroundWorker worker = sender as BackgroundWorker;
worker.ReportProgress(10);
|
|
|
|
|
Thanks for the reply...
I have read afew articles regarding the backgroundworker.
I am not sure if I am asking the correct question here, but my purpose is to make the backgroundworker to do some checking every few miliseconds.
1-
It seems to me that the checking has to be done in the progresschanged event. Am I correct?
2-
Where do I set the time interval in the backgroundworker?
Thank you
|
|
|
|
|
The purpose of the background worker is to run a task in the background. The purpose of the progress changed event is to indicate that some progress has been made, so you'd typically use this to update the UI to indicate that a task is working successfully. I wouldn't use a background worker for the purpose you want to - you'd be better off using a timer.
|
|
|
|
|
I did think about a timer at first but I do not think it is a good decision because of the following:
1-
There is a separate event which gets fired everytime a new message is received (i.e. every few milliseconds).
At this time, a collection is populated with the data being recieved in the above event.
2-
I am thinking of having a separate thread in the windows service to constantly check the collection and delete items where necessary...
What do you think?
Thanks
|
|
|
|