Image did not come out when run the program - c#

i already create a picturebox and have image inside it. But when i run the program, the image didn't show anything, just sign "x" (didn't load) when i run the program.
Where do i did wrong?
Here is the code:
private void Images(object sender, EventArgs e)
{
PictureBox pb1 = new PictureBox();
pb1.ImageLocation = "SamuderaJayaMotor.png";
pb1.Location = new Point(100, 100);
pb1.SizeMode = PictureBoxSizeMode.StretchImage;
pb1.Size = new Size(200, 200);
this.Controls.Add(pb1);
}
Here is the screenshot when i run the program:

You need to make sure that the image is in the same directory as the executable. That is, whilst you are creating your application, it will live in the /bin/Debug folder. If you are running in Release mode, it will be in the /bin/Release folder.
By supplying just a file name, your application is attempting to load the image from the current working directory. If you run your application in C:\MyApp.. then your image must also be inside C:\MyApp. If you are running it from D:\Projects\MyApp, then your image must also be in D:\Projects\MyApp.
What you can do.. is set your image to always copy to where your executable is. Right click on the image in the Solution Explorer and go to Properties. Then set the following property to "Copy always":
This will make sure the image is placed into the directory that your application is built in.

Related

C# Winforms change taskbar icon

I have an old legacy system that uses Winforms and is published with the built in One click,
I look after 3 different installs and each one has its own database it connects to, so its been set up that when a different system is click, it check isCompany1 and will set the Favicon accordingly
so when depoyment is done in the application window the Icon and Manifest is set to the correct ICO, when installed the .exe icon will be what is set there,
However i am trying to bring these all inline as when we debug a drop asks which database we would like to connect to, and depending on that it will set things up differently.
When running in VS the icons in the taskbar will change accoring to the dynamic Favicon however when its depolyed with this new selection it wont change, (it will change once on first load, then wont again)
The issue is some users need access to the different instances so would like different icons at the bottom, but its not changing the task menu, but everything else such as ALT-TAB and the control panel icon is changing,
all forms link into a baseform and call this :
if (App.IsCompany1)
{
this.Icon = new Icon("Resources\\Company1.ico");
}
else if (App.Company2)
{
this.Icon = new Icon("Resources\\Company2.ico");
}
else if (App.Company3)
{
this.Icon = new Icon("Resources\\Company3.ico");
}
AS i said this will change everything but not the Taskbar, but will from VS debugging,
I have made sure the .ico has all sizes, by writing their sizes on each one, and they display correctly the Taskbar and ALT-TAB both use 32x32
If it's any consolation, I couldn't reproduce your complaint.
I put 3 icons in resources, a single button on a form, this code:
private int iconum = 0;
private Icon[] icons = new[] { Properties.Resources.icon1, Properties.Resources.icon2, Properties.Resources.icon3 };
private void button1_Click(object sender, EventArgs e)
{
this.Icon = icons[iconum++ % icons.Length];
}
And it cycled through the icons in the main form title bar and the windows task bar over and over on every button click (made sure to run a release built exe too, not a debug start):
Note: the thing in the top right is my taskbar

Icon change works when debug, doesnt works when exe launched directly

I got little timer applicaton and i need to display current minute in its icon. So i draw couple of icons, put them into resources file.
Code for changing icon
int currentMinute = Int32.Parse(label1.Text[0].ToString());
if (currentMinute != globals.currentIcon)
{
ResourceManager rm = new ResourceManager("PoE7MinTimerLolz.Properties.Resources", Assembly.GetExecutingAssembly());
this.Icon = (Icon)rm.GetObject("_" + currentMinute);
globals.currentIcon = currentMinute;
}
Works perfectly when i debug application
But when i launch it from compiled exe file, icon doesn't changes from default
What do i do wrong?

WPF Application Relative path to a mp3 sound file

I'm building a WPF project with Visual Studio 2012 and I'm finding a problem with relative paths.
In the specific, I want a sound to be played when a ToggleButton is checked.
This is the code for my button:
<ToggleButton Name="Circles" Margin="88,104,86,172" Background="#FFFFD0CD" Foreground="Black" Checked="Circles_Checked">Circles</ToggleButton>
and if I write like this:
private void Circles_Checked(object sender, System.Windows.RoutedEventArgs e)
{
MediaPlayer mplayer = new MediaPlayer();
mplayer.Open(new Uri("C:\\Users\\user1\\Documents\\Visual Studio 2012\\Projects\\RehabilitationGUIProject\\RehabilitationGUIProject\\circles.mp3", UriKind.Relative));
mplayer.Play();
}
everything works as espected. But I want this code to work also onto other machines, not only mine. And what If I have to move the sounds to another location? I would like to use relative paths and move every sound inside my project folder.
So I created a new folder named Sound inside my root project, the structure is like this:
RehabilitationGuiProject
\Properties
\References
\Bin
\Object
\Sounds
\circles.mp3
... other files .xaml
So I wrote the same lines of code above, changing this one:
mplayer.Open(new Uri(#"/Sounds/circles.mp3", UriKind.Relative));
But no sound is played. Which is the correct relative path for playing sounds from a project folder?
The path must be relative to the folder where your application is executed.
mplayer.Open(new Uri(#"../../Sounds/circles.mp3", UriKind.Relative));
(Assuming your exe is run from the Bin/Debug folder)

I want to load default image in picturebox if the user does not select an image in C#

I am developing a window form in C#. In my window form there is a picture box. I want if the user does not select an image then the default image will be load in picture box which is save in my project folder.
thanks in advance
I guess you want to know how you can get the picture from the project folder right?
First add the picture to you project (add existing item) and set the Build Action to Embedded Resource:
then the following code do the trick:
private void SetPicture()
{
var assembly = System.Reflection.Assembly.GetExecutingAssembly();
using (var imgStream = assembly.GetManifestResourceStream("DataGrid.TestImage.jpg"))
{
var img = new Bitmap(imgStream);
Picturebox.Image = img;
}
}
where "DataGrid" is MY project-name (you have to insert your own) and "TestImage.jpg" is the name of your Image (if you put it into a folder you might have to give the foldername too).
Picturebox is a PictureBox-Control I set the image to.
Either set an image in the designer and override it in code
for instance you can place this after user selection:
if (someConditionIndicatingUserAction)
pictureBoxControlName.Image = UserSelectedImage;
Or, you can store a default value in a variable and set the picturebox image to it (if the user selects nothing, you don't change the value, and if he does, change it accordingly)

Programmatically change the icon of the executable

I am developing an application called WeatherBar. Its main functionality is based on its interaction with the Windows 7 taskbar — it changes the icon depending on the weather conditions in a specific location.
The icons I am using in the application are all stored in a compiled native resource file (.res) — I am using it instead of the embedded resource manifest for icons only. By default, I modify the Icon property of the main form to change the icons accordingly and it works fine, as long as the icon is not pinned to the taskbar. When it gets pinned, the icon in the taskbar automatically switches to the default one for the executable (with index 0 in the resource file).
After doing a little bit of research, I figured that a way to change the icon would be changing the shortcut icon (as all pinned applications are actually shortcuts stored in the user folder). But it didn't work.
I assume that I need to change the icon for the executable, and therefore use UpdateResource, but I am not entirely sure about this. My executable is not digitally signed, so it shouldn't be an issue modifying it.
What would be the way to solve this issue?
If you want to do this programatically, I would start by looking at the Portable Executable file format (Wikipedia entry). The resources section (.rsrc, see section 6.9) should contain the icon. Using this information, you can write a tool to modify the icon.
If you just want to quickly change an icon in an existing file, you might be able to hack it up in the Visual Studio resource editor. I tested this with a file by deleting the old icon and adding a new one. The .exe icon changed in Explorer to the new icon, and the new icon appeared on the Start menu when I dragged it there.
-- Edit --
Yes, I agree that using UpdateResource is a good approach. Here is an example I found of using C++ functions to do so, and a P/Invoke signature for UpdateResource and FindResource.
private void button1_Click(object sender, EventArgs e)
{
String path = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
String name = "test";
Shell32.Shell shl = new Shell32.ShellClass();
// Optional code to create the shortcut
System.IO.StreamWriter sw = new System.IO.StreamWriter(path + #"\" + name + ".lnk", false);
sw.Close();
// End optional code
Shell32.Folder dir = shl.NameSpace(path);
Shell32.FolderItem itm = dir.Items().Item(name + ".lnk");
Shell32.ShellLinkObject lnk = (Shell32.ShellLinkObject)itm.GetLink;
// Optional code to create the shortcut
lnk.Path = Environment.GetFolderPath(Environment.SpecialFolder.System)
+ #"\notepad.exe";
lnk.Description = "nobugz was here";
lnk.Arguments = #"c:\sample.txt";
lnk.WorkingDirectory = #"c:\";
// End optional code
lnk.SetIconLocation(Environment.GetFolderPath(Environment.SpecialFolder.System)
+ "cmd.exe", 1);
lnk.Save(null);
}
This was taken from http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/9e23a82c-8bed-4b96-8b9a-4c2b6136a622/
It may help.
I decided to implement a workaround - the icon will change in the thumbnail for the window (it is possible in Windows 7). If the icon is unpinned, the user can see the icon changing. In case it is pinned, the thumbnail will change according to the current weather conditions.
Seems to me like the structure of pinned icons (being a shortcut, in fact) doesn't allow dynamic icon change. If I am wrong, I am open for comments and ideas on this.

Categories

Resources