In user control designer I know how to add background image.
My image (16x16px) is located in user control. How to add this image in code?
How do I embed the image in user control so that when I bring the control to another project, the image also appears?
private Bitmap buttonResetImage = null;// <-- here I want to load my initial image.Then the user can change this image as he wishes;
public Bitmap ButtonResetImage
{
get { return buttonResetImage; }
set
{
buttonResetImage = value;
btnReset.BackgroundImage = buttonResetImage;
btnReset.BackgroundImageLayout = ImageLayout.Center;
}
}
Well since MDP does not respond I found the trick. I need to initialize this:
private Bitmap buttonResetImage = global::SliderControl.Properties.Resources.Reset;
If I use only:
private Bitmap buttonResetImage = SliderControl.Properties.Resources.Reset;
I get this error:
Error CS0117 'SliderControl' does not contain a definition for 'Properties' SliderControl
https://i.imgur.com/az52MOP.png
In order to create a user control that has any kind of resources like :
images
fonts
audio
icon
and etc, you can create them in a separate project (called Windows Forms Control Library) and add resources in it.
after all, the result(in this case, a dll file) contains all the resources and code base you need.you can use it in any where you want.
Related
I have a slim'd down IDE inside my application to allow some basic customization of a UI page. My toolbox contains some standard WPF controls. I am however having a hard time getting the Icon associated with any of the default WPF controls, I assumed they would have been defined by ToolboxBitmapAttribute just like user controls, but I am not able to see any such attribute on the types.
The following alwayse returns null on "default" WPF types such as Grid, Image, Border etc...
public Image Icon
{
get
{
Image controlImage = null;
AttributeCollection attrCol =
TypeDescriptor.GetAttributes(Type);
ToolboxBitmapAttribute imageAttr = (ToolboxBitmapAttribute)
attrCol[typeof(ToolboxBitmapAttribute)];
if (imageAttr != null)
{
controlImage = imageAttr.GetImage(Type);
}
return controlImage;
}
}
How do you use a button to send an image within the same page to another page in the application via the button click event?
In the method that is called on the button click, I'd use the application properties to store your image. Something like this (you will want to put a data type on your image):
Application.Current.Properties.Add("myImage", System.Windows.Media.Imaging.BitmapImage myBmp);
Window newWindow = new Window();
newWindow.Show();
Then on the window that opens, grab the image from the app properties like this (note that you'll want to cast your image as they image type you want):
transferredImage = (MYIMAGETYPE)Application.Current.Properties["myImage"];
var brush = new ImageBrush();
if (image != null)
{
brush.ImageSource = image;
btnCustomerPhoto.Background = brush;
}
Application.Current.Properties.Clear();
Of course, you don't have to clear the current properties if you don't want to, but I do to ensure I don't have anything sticking around I don't want.
There are other ways to do this but the application properties are easy to use and then get rid of.
My project consists of a form with a panel that contains a custom control.
In this custom control I have buttons that change the background image.
My issue is; these buttons only change the background image of the custom control that they are placed in, and I need them to change the background image of the main form containing the panel with the custom control.
My current code:
this.BackgroundImage = Image.FromFile(System.IO.File.ReadAllText(BackgroundSkinsPath));
I need something that will in effect accomplish this:
MainForm.BackgroundImage = Image.FromFile(System.IO.File.ReadAllText(BackgroundSkinsPath));
ie: Change background image of MainForm.cs from CustomControl.cs
You can use Control.FindForm method for that, like this
this.FindForm().BackgroundImage = ...
I ended up using something different:
Form MainForm = Application.OpenForms["(The name of the form in which I wanted to change the background)"];
//...
MainForm.BackgroundImage = Image.FromFile(System.IO.File.ReadAllText(BackgroundSkinsPath));
It ended up being a lot simpler than I thought it was.
Find the control's parents and change the BackgroundImage:
if(this.Parent!=null && this.Parent.Parent!=null)
this.Parent.parent.BackgroundImage = Image.FromFile(System.IO.File.ReadAllText(BackgroundSkinsPath));
I am using WPF-Xaml I made interface in which user selects image and save it in database, then this saved image assigned to buttons ImageSource and similarly to all other Navigation Panel items, etc.
Problem is that: i have to retrieve image every time from database, Is there any other possibility to save image without connectivity to database i mean Application Settings ?
i am not using MVVM
Create a property in your view model that will hold this image:
public class ViewModel{
public ImageSource SharedImage{get;set;}
}
Then bind your controls that need to show this image to this property. This way the image is set once and used my times by other controls that need to access it.
EDIT
If you want to cache your file locally in your file system when your application first uses the image you could do the following:
public class ViewModel{
private ImageSource _sharedImage;
public ImageSource SharedImage{
get{
if(_sharedImage== null){
// Go load the image from somewhere
}
return _sharedImage;
}
set{
if(_sharedImage == null){
// Store the image contained in value into your local cache
}
_sharedImage = value;
}
}
}
I created a simple windows form application C#, that is supposed to show a picture. I am following a tutorial from here Following is the code of form1.cs
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
// Construct an image object from a file in the local directory.
// ... This file must exist in the solution.
Image image = Image.FromFile("Picture1.png");
// Set the PictureBox image property to this image.
// ... Then, adjust its height and width properties.
pictureBox1.Image = image;
pictureBox1.Height = image.Height;
pictureBox1.Width = image.Width;
}
}
The image is present in the solution folder, but still nothing is displayed in the window when the application is run. There is no compilation error.
Update
Its solved now.
Make sure the PictureBox click event handler is registrated. It is not sufficient to copy the example code. (I'm just guessing)
Without a full path, the image won't load from the solution directory.. it will load from the directory where your executable is executed from.. which is either the Debug or Release folders when you're running from Visual Studio.. depending on what Profile you're running it with.
So put the image in the /bin/Debug folder and run your program again.
A better option would be to add this image to the project and do either of the two:
Add as a resource. It can later be used with strong typing.
Set this option on the file: Copy to Output Directory = Copy if newer.
It is not a good practice to add anything manually under /bin/, because those folders are volatile and can be erased by Visual Studio at any moment (usually on the next rebuild).