Dynamically change listview icon size in c# - c#

I'm trying to make a listview with options. One of the option is to change icon size. I tried to make the change like this :
this.listview1.LargeImageList.ImageSize = this.list.SmallImageList.ImageSize = new Size(h, w);
This changed the image size, but the image disappeared. Can anyone help me ?

Please take a look at the following link, I think it should be useful:
How to Change existing Imagelist image size of Listview
use the following code. it works for me :
listView1.View = View.LargeIcon;
ImageList iList = new ImageList();
iList.ImageSize = new Size(128, 128);
iList.ColorDepth = ColorDepth.Depth32Bit;
iList.Images.Add(Properties.Resources.x64_Clear_icon);
listView1.LargeImageList = iList;

Related

How to write text in Listview

This is my code to write text with a image in list view.
var imageList = new ImageList();
Image image = Image.FromFile("ABC.png");
imageList.Images.Add("ABC", image);
listView1.LargeImageList = imageList;
var listViewItem = listView1.Items.Add("text with image");
listViewItem.ImageKey = "ABC";
The issue I am facing is the text and the image should come in one row but currently they are coming in multiple rows.
Thanks to #Klaus Gütter I was able to solve this.
I changed the view from LargeIcons to SmallIcons.
Also did change this line in the code.
listView1.SmallImageList = imageList;

Adding a group of images to ListView

I have a ListView and a List<Image> (there are 3 images in the list). I'm trying to add all the images to the ListView and currently this is what I have:
foreach (Image image in ListOfImages)
{
PictureBox pictureBox = new PictureBox();
pictureBox.BackColor = Color.Gray;
pictureBox.Width = 200;
pictureBox.Height = 200;
pictureBox.Image = image;
pictureBox.SizeMode = PictureBoxSizeMode.Zoom;
listView1.Controls.Add(pictureBox);
}
And this is what it looks like in the end:
That image is the first one in my list. The other two aren't visible. Is there a reason why the others aren't showing up?
I think microsoft has a better solution to offer - ImageList.
Why can't you use the ImageList?
I can help you one that
First create an Imagelist control from Toolbox.
Set your istview's imagelist.
e.g. listView1.LargeImageList = imageList1;
foreach (Image image in ListOfImages)
{
imageList1.Images.Add(image);
}
Add list Items to your list control and mention the respective image index.
listView1.Items.Add(key, text, imageindex);
Hope this helps!
Ok, I've found that the correct answer for what I want to do is to not use a ListView but to instead use a FlowLayoutPanel. The code above will work exactly the same way, except instead of listView1 it should be flowLayoutPanel1 (or whatever the name of the FlowLayoutPanel is)

Populating winform ListView from Calling External Class, not adding image and text

I am trying to add a new image list with text describing each image below each image. I have set the listview to large icons and tried many other attempts to get preview images back into the first form.
I have been able to either
1. get very small images into the list view
2. get just the text into the listview
3. get the very tiny images again with text first then the images, not together.
I have tried all the overloaded methods and I am still at a loss as to why i can't even get a Properties.resources.image in as an image placeholder. except as a tiny image. And yes I have set the listview large icon mode and tried the imageSize properties, nothing works. Also some attempts usually with a technique that appears like it is following an explanation on here fails to show anything in the listview on the frstForm.
Any hints would be appreciated.
public ImageList imageList_c = new ImageList();
private Family_Loader_ExtEventDialog _frstForm;
foreach (KeyValuePair<string, Bitmap> kvp in element_Dict(_doc_new, BuiltInCategory.OST_Walls))
{
//imageList_c.ImageSize = new Size(120, 120);
//imageList_c.Images.Add(kvp.Key, Properties.Resources.folder);
_frstForm.listView_Family.Items.Add(kvp.Key);
}
//_frstForm.listView_Family.LargeImageList = imageList_c;
imageList_c.Images.Add(kvp.Key, kvp.Value);
_frstForm.listView_Family.View = System.Windows.Forms.View.LargeIcon;
_frstForm.listView_Family.LargeImageList = imageList_c;
for (int i = 0; i < imageList_c.Images.Count; i++)
{
ListViewItem item = new ListViewItem();
item.ImageIndex = i;
item.Text = imageList_c.Images.Keys[i];
_frstForm.listView_Family.Items.Add(item);
}

How to set ImageSource as Xamarin.Forms.Button?

I am trying add a background image using the image property in button. The issue I'm facing is that i can't set StreamImageSource as button background. I encountered the error given below if I try to do so.
The Code I use to set Image:
ImageSource iconsource =ImageSource.FromStream(() => new MemoryStream(ImgASBytes));
Button Icon = new Button ();
Icon.Image = iconsource ;
The Error I encounter:
Error CS0266: Cannot implicitly convert type 'Xamarin.Forms.ImageSource' to 'Xamarin.Forms.FileImageSource'. An explicit conversion exists (are you missing a cast?)
ImageSource.FromStream () returns a StreamImageSource (see docs). Button.Image accepts only FileImageSource (see docs).
It means that what you're trying to achieve won't work, no matter how hard you try to cast one into the other.
Button.Image will accept images stored as resources in your platform projects, and loaded either with:
Icon.Image = ImageSource.FromFile ("foobar.png");
or
Icon.Image = "foobar.png";
The accepted answer is true that you can't cast StreamImageSource to FileImageSource, I think that the real question is about how to share images in a PCL and use them on a button, just like one would when creating an Image forms control.
The answer is to have a Grid which contains both a Button and an Image object, where the Image overlaps the Button.
For example, the C# code might look like this:
ImageSource imageSource = ImageSource.FromStream(() => new MemoryStream(imageAsBytes));
Button iconButton = new Button ();
iconButton.VerticalOptions = LayoutOptions.FillAndExpand;
iconButton.HorizontalOptions = LayoutOptions.FillAndExpand;
var image = new Image();
image.Source = imageSource;
// So it doesn't eat up clicks that should go to the button:
image.InputTransparent = true;
// Give it a margin so it doesn't extend to the edge of the grid
image.Margin = new Thickness(10);
var grid = new Grid();
// If we don't set a width request, it may stretch horizontally in a stack
grid.WidthRequest = 48;
// Add the button first, so it is under the image...
grid.Children.Add(iconButton);
// ...then add the image
grid.Children.Add(image);
You may have to play with the sizes and thickness values but this should get you a clickable button with an icon.
As of Xamarin.Forms 3.4.0 you can now use ImageButton. You can use embedded images by using an extension method explained in this MS document
Careful with upper- and lowercase in filenames.
I was wondering, why my button-images were shown properly on the simulator, but not on my iPhone.
On the device the filename must match exactly, the simulator doesn't care about upper- and lowercase in filenames.
I use this and it works
var imageA = new Image();
imageA.Source=(FileImageSource)ImageSource.FromFile(allergeneLocation)};
or
var imageA = new Image()
{
BackgroundColor = Color.Teal,
Source = (FileImageSource)ImageSource.FromFile(allergeneLocation)},
};
Here is what I tried:
Button refreshBut = new Button
{
Image = (FileImageSource)
(ImageSource.FromFile("refreshBut.png"))
};
While it compiles I then get an unhandled null reference exception with the description: Object reference not set to an instance of an object. I am not sure if this will help anyone else try to solve this but I am at the same wall.

Change button background

After several attempts to change the background of a button, and getting errors of casting and things like that I finnaly got to this point:
Uri dir = new Uri("red_flag.png",UriKind.RelativeOrAbsolute);
ImageSource source = new System.Windows.Media.Imaging.BitmapImage(dir);
ImageBrush bru = new ImageBrush();
bru.ImageSource=source;
bru.Opacity = 100;
This code does not generate errors, but I can't see the changes when I call:
button1.background = bru;
It just makes anything! :(
found the answer myelf after reading Mick's answer, I share with you what I did:
Uri dir = new Uri("red_flag.png", UriKind.Relative);
ImageSource source = new System.Windows.Media.Imaging.BitmapImage(dir);
Image image = new Image();
image.Source = source;
StackPanel stack = new StackPanel();
stack.Children.Add(image);
myButton.Content = stack;
Thanks for your help
Update 1:
For best results set the padding property of your button to 0 (in each of the cases) so the image can resize automatically to fill all the button, please note this could hide your actual content, in my case this was what I wanted.
If this code is part of the click event handler for the same button you will have this problem.
Peter Torr explains why here and offers a solution.
Why can't I change the Background of my Button on a Click event?

Categories