How to assign one image to all image sources of buttons - c#

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;
}
}
}

Related

User Control: how to add background image on button in setter/getter

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.

Access ImageCollection of a UserControl in the Form Designer

I want to make a user control which acts like a TabControl but the tab page buttons should have big images. Therefore I want to have a property Images which adds images to the user control like an ImageList. For every image, the user control then generates a PictureButton and a corresponding panel. For that reason I added an ImageList to the user control and I've also tried to just add a collection of Image.
The problem is, that I cannot access this ImageList from the main form designer.
I've tried the following properties:
public Collection<Bitmap> Images { get; set; }
// ----
public ImageCollection Images { get; set; }
// ----
public List<Bitmap> Images { get; set; }
// ----
public ImageList Images { get; set; }
When I try to add an image to a Collection<Bitmap> or List<Bitmap> in the designer then Visual Studio crashes. I cannot use Image instead of Bitmap.
I know that I could use TabControl itself and give each TabPage an image, but I don't like the look of it.
So what's the best way to add a collection of images to a user control from the main form?

Retrieve the Toolbox Image associated with standard WPF controls

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;
}
}

dataGridView image column link to file

I am working with an image column on dataGridView:
As shown in the attached picture, when I click on 'browse' I can choose a file from the dialog window.
What I want...
is to make the image on the 'Files' image-column to link to the chosen file (to get the file open when the user clicks on the image).
I have no idea how to make that happen. Could anyone give me a direction?
First You can create your own Class and put in this class Property for File Path
I've created example :
public class MyClass
{
public string Name{get;set;}
public string FilePath{ get; set; }// Here i put The path and it will not appear in the Grid will just show the first item
}
in DataGrid there is an event called SelectionChanged you can add this code there
if (DG.CurrentColumn.Header.ToString() == "Name")
Process.Start((DG.SelectedItem as MyClass).FilePath);
when i select the item it will open the folder you can put instead of ("Name") your column name ("Brows")

Cycle Button Background Images in C#

I have a form in C# that has a button that, when clicked, I want the background image to cycle through a set of images (which I have as resources to the project). The images are named '_1', '_2', etc. and each time I click the button I want its background image to increment to the next one and go back to "_1" when it gets to the highest. Is there a way to do this?
I tried getting button1.BackgroundImage.ToString() but that yields System.Drawing.Bitmap instead of Resources._1 like I was thinking it would (in which case I could just get the last character and switch on that to change the background to the appropriate new image).
Thanks for your help.
Why don't you just put the images in an array?
You could subclass Button and override the BackgroundImage property so you can better keep track of the current resource that represents the image. You might also override the onclick method to internally handle cycling to the next image, though that might be a little weird if the resources are handled outside of your derived button class.
class YourClass
{
private IEnumerator<Image> enumerator;
YourClass(IEnumerable<Image> images)
{
enumerator = (from i in Enumerable.Range(0, int.Max)
from image in images
select image).GetEnumerator();
enumerator.MoveNext();
}
public Image CurrentImage { get { return enumerator.Current; } }
public void OnButtonClick() { enumerator.MoveNext(); }
}
You can use this code as a backing class for your control under the assumption that user wont click the button more than two billion times.
Just note that once this class is created you cannot modify given image list outside. If you want to do such things you need to implement disposable pattern and dispose the enumerator accordingly.

Categories