Using a resource image in a library different from the main project - c#

I have a WPF program with two libraries
now inside I have a gray resource background both in the main program and HelperLib. But additionally in the HelperLib I have a red background I want to use.
Now when I want to change the background of a window with:
switch (bubbleType)
{
case eBubbleType.ERROR:
bw.btText.Background = new ImageBrush(new BitmapImage(new Uri("pack://application:,,,/Resources/Images/gradientWallpaper_RED.jpg"))); <-----I want to use this one
break;
default:
bw.btText.Background = new ImageBrush(new BitmapImage(new Uri("pack://application:,,,/Resources/Images/gradientWallpaper.jpg")));
break;
}
but I get an image not found exception on the RED image but not on the other one.
I suspect that, when using the gray gradient it's not using the one in the lib but the one in the main program since I see that the resource is related to the assembly not to the project.
Both images have the same properties as in picture:
Thanks for helping

You would have to use a Resource File Pack Uri with the name of the referenced assembly:
new Uri(
"pack://application:,,,/HelperLib;component/Resources/Images/gradientWallpaper_RED.jpg")

Related

Resource-Image usability for a Canvas background

I'll be able to program a short program where you can click on a canvas and you'll get the RGB color value for the clicked pixel.
At first i had the graphics on my own HDD with absolute paths.
Problem is, that the program will crashed on each other pc ;)
So, i had created a folder named "Recourcen" within the Project explorer.
Inserted are all the graphics i need.
If i define some images in wpf and write it as follow
...Source="/Resourcen/color_wheel_730.png"
The Image get the right source.
But if i want to use it for a canvas in source, it doesn't run without getting errors.#
canvas_colorpick.Background = new ImageBrush
{
ImageSource = new BitmapImage(new Uri(#"..\..\Resourcen\color_wheel_730.png", UriKind.Relative))
};
I had tested some formattings, but nothing will work in the source code section :(
After some hours of testing i thought i must be put a question to these forum.
Hope somebody can help me with my noob question above.
Greetings from Germany!
Bjoern
Make sure you have set the BuildAction as Resource for added image file in your project.
Also, try using WPF Pack URI to provide the uri source:
canvas_colorpick.Background = new ImageBrush
{
ImageSource = new BitmapImage(new Uri(#"pack://application:,,,/Resourcen/color_wheel_730.png"))
};
Note - Assuming folder Resourcen folder added in same project where XAML resides.

how to change background image of windows store application in c# code?

I want to change the background image of a windows store app using C#.
I'm looking to change the background image similar to the following pseudo code:
This.Background.ImageSource= "dracula-128.png";
One way is to create an ImageBrush, set its ImageSource property, and then assign that to the background. (Code updated based on comment)
//BitmapImage class is within this namespace
using Windows.UI.Xaml.Media.Imaging;
ImageBrush ib = new ImageBrush();
ib.ImageSource = new BitmapImage(new Uri(#"ms-appx:///dracula-128.png", UriKind.RelativeOrAbsolute));
this.Background = ib;

How to change image location

I'm developing WPF with C# and I have this code to get the picture angry.png from the smiles folder in the output folder (e.g. bin\debug):
BitmapImage bitmap = new BitmapImage(
new Uri(#"smiles/angry.png",
UriKind.RelativeOrAbsolute));
But I want to get the picture angry.png from another location like Resources\Image\smiles\angry.png. Could you tell me how to change the folder location so that this would work?
In that case ..\..\Resources\Image\smiles\angry.png should be sufficient.

Convert SWF to PNG image with transparency

I'm converting a swf file to png image using SWFtoImage tool in c#. I'm able to convert the file successfully, but the problem is transparency. The converted image contains a black background instead of transparent, as you can see in the attached image.
how can I fix this? since the tool fails to create a transparent image, is it possible in .net to manipulate on the converted png image and make its background transparent?
Current Code:
//Namespace: BytescoutSWFToVideo;
// Create an instance of SWFToVideo ActiveX object
SWFToVideo converter = new SWFToVideo();
// Register SWFToVideo
converter.RegistrationName = "demo";
converter.RegistrationKey = "demo";
// set input SWF file
converter.InputSWFFileName = #"D:\image2069.swf";
// Enable trasparency
converter.RGBAMode = true;
// Extract all frames to .\Output sub-folder
converter.ConvertAllToPNG(#"D:\Result\");
This is the swf file.
I don't know much about SWFToImage. But the company's web site contains demo code with the following comment (translated to C#):
// Enable trasparency - set BEFORE setting input SWF filename
converter.RGBAMode = true;
So possibly you have to change the order of your statements.

Setting the form icon for an XNA window?

Is there any way of changing the icon for an XNA game form (i.e. the one that appears in the top left corner of the form, and on the taskbar)?
I currently have a multi-icon with a variety of sizes embedded, including a 16x16 one. Unfortunately the project property "Application -> Resources -> Icon and manifest" uses the 32x32 one and scales it down, rather than the native one.
I'd like to manually set the icon to use the 16x16 one contained in the icon I have, and if possible change it dynamically at run-time.
Thank you.
After reading this thread, I came up with the following code (from program.cs):
static void Main(string[] args)
{
using (Game1 game = new Game1())
{
((System.Windows.Forms.Form)System.Windows.Forms.Form.FromHandle(game.Window.Handle)).Icon = new System.Drawing.Icon("file.ico");
game.Run();
}
}
file.ico should be copied to the same place as your executable.
You have to reference these assemblies in your solution:
System.Windows.Forms
System.Drawing
You can also replace the Game.ico in your solution folder, which has same effect.

Categories

Resources