Errors after changing tool strip button image - c#

Once i add this line to the code:
this.tsbAdd.Image = Bitmap.FromFile(#"..\..\Resources\add.bmp");
I'm unable to open editor of that form.
Screenshot of designer
I can compile app and images work as they should.
Expected results - new image is displayed without breaking designer.
Real results - new image breaks designer.
Once i build it into .exe it doesn't open. Without images it works flawlessly.

Nope it won't. The picture will be built, but referencing it by this path won't be working.
The resource file WOULD be built into your exe, but not at "....\Resources\add.bmp". This path only exists in your IDE configuration position, when your program is at "bin\Debug", you understand me?
Imagine you put your exe into C:\, then where is "....\Resources"? You cannot refer to a image in this way.
You should add resources in the project panel (I believe you have done that), and the way you get this file is via ResourceManager, not using this path. Like this:
ResourceManager rm = Resources.ResourceManager;
this.tsbAdd.Image = (Image) rm.GetObject("add");
The resourcemanager will pull the resource bitmap out from your built exe. Just using that path won't work. As the designer is not ran in \bin\Debug, no wonder it is broken too because it cannot find your file using that path.

Related

How to constantly get same directory path from inner directory

I am using c#.
I have a project running at path:
D:\USER\Desktop\Prgramming\C++ 2020-2021\project\projectGraphics\projectGraphics\bin\Debug
The Project directory looks like this:
I want to get constantly (in any computer) from an .exe file in Debug to an image in Resources efficiently. How can I do that?
Do you use WinForms or WPF?
Add the image as a "resource" in your project.
Afterwards you can use
Bitmap bmp = new Bitmap(Properties.Resources.myImageFile);
to access the image.

Getting Image to display in form - dll file

I have a vb.net form application in which i have displayed a logo from Application startup path using the code
Application.StartupPath + ("\Image\" + "Logo.jpg")
This works and the image displayed as expected. This exe is called from jquery using AciveX and hence it works only in IE.
I have converted the vb.net form application to dll by referencing the exe created. Now, when i call the dll function from another application, the form appears but the logo is not displaying.
I have used edge to read dll files from node.js application.
Please help to get the image to be displayed in the form when reading using dll file.
Did you check your image path at runtime to ensure it was resolving correctly? If your dll is in a different folder than your calling application, Application.StartupPath may not resolve as expected.
I was having a similar problem, but referencing an image in my resources manager. I realized when I transferred the form from an application solution to the dll solution, my PictureBox lost the default setting of its "Image" property in the designer, so it wasn't trying to set any image at all.

C# + XNA: Location of Sprite Folder and Sprites after Compile

I am trying to program an automatic Resource Loader for my project and so it is going well. But I am stuck on the loading of the sprites. I used this for checking the file exists
if (File.Exists(contentManager.RootDirectory + "/Sprites/" + spriteName + ".png"))
However it never finds the file, even though the file exists in the Content of the project. I have checked the folders and found that the .pngs are replaced by .xnb files. So I tried changing the file extension and then was given an error in the Sprite Load function
return contentManager.Load<Texture2D>(assetName);
It only happens if I change it from being a typical image file (png, bmp, etc). Can someone explain why this is the problem and how I can get around this? I have had no experience in the xna file extensions and can not find the answers I need. What I want is to load a new image into the project providing it exists in the Content Directory after compile
While on the topic, will the Sounds and Music be effected by the Compiler?
When you use a content project, the resources are imported, processed and write to disk again as xnb files.
If you want to dynamically load resources you have to compile files on the fly through the content builder or avoid using the content project.
Microsoft provides examples like this to load content dynamically: http://xbox.create.msdn.com/en-US/education/catalog/sample/winforms_series_2
if you want to distribute the application, the runtime xna is not enoigh, you need full xna framework and visual studio installed to work.
if you want the original files in the content project, you have to change their "copy to output folder" property to "copy always", and realize that the compiled results will be at "project folder\bin\debug\content"

Assign an external image to a Picturebox in Visual Studio 2008 (C#)?

How can I assign an external image into the PictureBox in Visual Studio 2008 ?
Typically, When we use ChooseImage in PictureBox , Visual Studio adds the image to the exe file and it causes increasing exe file's volume, I wanna add the image from a directory beside the exe file.
Is it possible in Visual Studio 2008?
P.S:
I don't want add the image with C# code, because VS2008 doesn't show it in developing time.
I think you can do this in C# code using the Image.FromFile method. As long as the C# code is in the initialization of the form / control, it should run at both design time and run time.
You can use Linked resources.
When you add a linked resource, the
.resx file that stores your project
resource information includes only a
relative path to the resource file on
disk. If you add images, videos, or
other complex files as linked
resources, you can edit them using a
default editor that you associate with
that file type in the Resource
Designer. When you add an embedded
resource, the data is stored directly
in the project's resource (.resx)
file. Strings can only be stored as
embedded resources.embedded resources.
MSDN on Linked vs. Embedded Resources
P.S. I however still don't get why would you need it—even if .exe doesn't get bigger, the whole app package would have a bigger size, wouldn't it?
Best,
Dan
If this is WinForms, it's better to just set the ImageLocation of the PictureBox to the path to your image. Do this from the designer to have it show at design time.
You can get images from Resources
pictureBox1.Image = Properties.Resources.[image name];
see How to embed and access resources by using Visual C#
Misread the question OP asked to load without using the Resources.
you will need to do
Image.FromFile("c:\\image.bmp")
If image if a JPEG then You can cast it to Bitmap
Bitmap myJpeg=(Bitmap) Image.FromFile("myJpegPath");
OR,
System.Drawing.Image loadImg = System.Drawing.Image.FromFile("c:\\image.JPG");
loadImg.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);

Loading Content images from Blend from separate project

I have a VS2010 solution with 5 projects. Two of these projects are called:
MyResources
Application
The Application project contains all of the views and viewmodels for the application to run, and the Resources project contains all the images, resource dictionaries, etc for theming and skinning.
The way I have images set up in Resources, I have their build action set to Content, and Copy set to Copy Always. In my Application project, I reference these files using a pack URI. Example:
"pack://siteoforigin:,,,/Themes/DefaultTheme/BackgroundImage.png"
This works fine for me. The themes folder and all its subfolders are properly copied to the proper folder since I'm using siteoforigin://.
However, this breaks Blendability. When I load the project in Blend 4, I get errors.
If I use siteoforigin, Blend tries to load the images from the executing folder of blend.exe (In this case, C:\Program Files\Microsoft Expression\Blend 4 Beta\Themes\Default_Theme\BackgroundImage.png")
If I change the pack URI to the following:
pack://application:,,,/IQ.IQKiosk.Resources;component/Themes/DefaultTheme/BackgroundImage.png
It tries to look for the image in "themes/default_theme/backgroundimage.png"
If I try:
/MyResources;component/Themes/DefaultTheme/BackgroundImage.png
It tries to look for the image in "C:\MyResources;component\Themes\DefaultTheme\BackgroundImage.png"
If I try:
/Themes/DefaultTheme/BackgroundImage.png
It tries to look for the image in "C:\Themes\DefaultTheme\BackgroundImage.png".
So now I'm stuck. I'm not sure how to reference my images without having to embed them into the resource project, and I can't hard code the directory because my other coworkers have the project in different folders as well.
I tried to think of a way to get the proper URI to the proper locations of the files (relative to the .sln file would be awesome) or have the files get copied over to where blend compiles and executes the project. Alas, I am unable to figure out how.
Does anyone know the proper way to get these images to load?
I figured it out. Instead of using a pack URI, I just used
new Uri(Environment.CurrentDirectory + "\\Themes\\DefaultTheme\\BackgroundImage.png")
and it found the proper folder, since Evironment.CurrentDirectory gave me the proper bin folder where my themes were copied to.
Huzzah!

Categories