Image shrinking in a label of c# win forms - c#

I am designing a c# .NET 4.5 win form.While inserting an image in a label, the image is behaving differently.I have two labels and i insert 2 different image in them.In the first one, i am setting image using an Image property and in the second one, i am just using the ImageList.Now the funny thing is that for the second label, its image is shrinking while for the first label its not. Why is it happening so??Please advise. Image is attached herewith for better understanding:
In the image, you can see that image of first label is showing correctly, while that of second label not

The issue is that the Image and ImageList properties work pretty differently. While for the Image, it will display the image as it is, ImageList has a property called ImageSize. This property affects the size of the stored images in the Label ImageList at compile time such that all images in the ImageList will have the size of ImageSize in the runtime.
Your image shrinking in when you use ImageList is likely caused by this. The default value of ImageSize is 16x16.
And you also cannot change the ImageSize at runtime, since it will be like replacing the list, all images will not be displayed(!).
So, if you display your images using ImageList, all your images must be of the same size. Or else, you have to put your images in the Resources.resx such that the image size will not be changed and somehow access to the images there at run time.

Related

Save original image from PictureBox, not resized

Is it possible to save original image with original quality from PictureBox, not the stretched/resized one? I download byte array from a server and put it in PictureBox, I want user to be able to save the actual image, but it should be the original image (original quality and original size) I received from the HTTP server, not resized and reduced quality one that I show in a PictureBox. Is it possible or do I have to store the byte array I receive from server somewhere in order to achieve this?
Image property of the PictureBox contains the original image, while the control may paint the image as zoom/resized/stretched depending to its SizeMode. You can see the source code for the property here.
Just save the Image property and it will be the same image which you had read from database.
So calling pictureBox1.Image.Save will be enough.

Replace an image when clicked

Not sure if the title makes any sense.
In my WPF application I would like the window to contain a small image, icon size. When the user clicks on the image another one simply replaces it, that holds the same dimensions.
I have all the images loaded into my project for C#/WPF. (By the way there are 3 images)
What I have been trying:
I tried changing the opacity in the code-behind to make one image
have full opacity and the others have no opacity. Didn't work as the
first toggle would strangely make all images disappear.
I also tried dynamically changing the image source in the code-behind. I
used if statements and a field to determine what image to switch
the source to. Code being:
if (toggle == 1)
{
thebutton.Source = new BitmapImage(new Uri(#"/images/icon2.png", UriKind.Relative));
toggle = 2;
}
Also did not work (made it blank, rather than switching to another image) but I feel like there is an obvious way I'm not seeing.
What I initially wanted to do was simply (like in graphics programs) raise and lower the images to the top and bottom to determine which ones the user should see. All that matters is the visual. The user clicks image 1, image 1 disappears and image 2 appears in its place, and so on for image 3.
Set the Build Action of the image files to Resource, and load them by Resource File Pack URIs:
thebutton.Source = new BitmapImage(new Uri("pack://application:,,,/images/icon2.png"));

How to display scaled image without anti-aliasing?

Question
How can I scale an image in XAML quickly without anti-aliasing applied?
Background
I am trying to make a pixel editor as a Windows 8 XAML/C# app. I'm using c#/XAML because most of my experience is with c#/WPF.
Method 1: WriteableBitmap + Image control. Originally, I used a WriteableBitmap to store and edit an image. That image is displayed in a resized XAML Image control. The problem is that the image does not get scaled properly because of anti-aliasing. (XAML does not seem to provide the BitmapScalingOptions that are available in WPF)
Method 2: Redrawn WriteableBitmap + Image control. Next I tried writing my own scaling, where I take my original image and write to a larger WriteableBitmap so that the scaled image is pixelated. The scaled image is then presented inside a XAML Image control. This process is slow and inefficient.
Method 3: SharpDX + Direct2d ?? I am pretty sure a solution exists somewhere between SharpDx, SurfaceImageSource, and Direct2d. Event still, I can't quite figure out how to display a SharpDx.WIC.Bitmap inside a Windows.UI.Xaml Image control, and am generally getting lost in the documentation.
What exactly is a recommended setup for achieving my desired end? Are there c# samples available which might point me in the right direction?
I don't know if you mean by scaling it by resizing it with the mouse or just with a button click to auto scale to a predetermined size.
But the thing you can do is to use an Image panel and then, just set it to image.Stretch = Stretch.Fill; so if you override and create a method for the Resize event of the Image Panel, your image will take the size to fill the Image panel.

imagelist with label, why does it keep shrinking images

I am using an imagelist with a label control, basically doing custom rolleover affect. However, if I use an imagelist and try and write the image to the label it keeps shrinking the image to 16x16, even when the image in the list is actually 21 high by 65 wide, why is this and how can I stop it, basically display image at its default size?
The problem comes from the ImageList. It has a property ImageSize that has a big impact on the List itself.
If you take a look at design time into the images of your ImageList, they will all show up their real width and height values. But by compile time the images will be stored within the list using the given ImageSize. So all images within a ImageList will be already stretched to the given size at runtime.
Attention: If you change the ImageSize at runtime all images will be removed from the list!
To solve your problem you have two possibilities:
If all of your images are of the same size, set this size to your ImageList at design-time.
If you have images of different sizes you should add them to your Resources.resx file and access them from there, cause here they will be saved as is and access is also fairly easy like Properties.Resources.MyPictureFile

Preventing color bleeding with StateImageList on TreeView

Does anyone have a solution for the color bleeding when using a StateImageList with a Windows Forms TreeView?
Using the same ImageList with assigned to the TreeView's ImageList property, results in correct rendering as can be seen by the following image.
The state images are left, normal images are right. All images are sourced from the same ImageList instance.
Update:
I converted the images to 16 color indexed palette. Seems it does not like that either. Its a bit better, but still far from acceptable.
The documentation on the .ColorDepth property of the .StateImageList states:
In the .NET Framework version 1.1 or
later, the default is Depth8Bit.
It looks like this 'default' is not allowed to be changed. Well, you can change it, but it doesn't do any good.
Looking at the code in Reflector, when the StateImageList property of the the TreeView is set, a new image list is created internally. This image list uses the default value for color depth. And since you cannot change the color depth after creating the imagelist, this pretty much guarantees that the StateImageList can only be displayed at the default color depth.
EDIT:
I think I've figured out what's going on here. I have been playing around with images like your shaded circles at varying color depths. Basically, when I take a decent image and convert it to 4-bit color, I get results that look exactly like the images from the StateImageList. Despite the '8-bit' note in the documentation, it appears that the StateImageList is still using 4-bit color depth (the old, old 16-color standard) as the default.
It is only when I use 4-bit images to start with in the imagelist, that I get no changes between the display of the imagelist and stateimagelist.
alt text http://www.freeimagehosting.net/uploads/74873abc94.png
I created this image for the imagelist using the old Windows 16-color palette. It looks absolutely terrible, but it didn't change in the stateimagelist.
Looks like the images are reduced in bit depth.
Is the ColorDepth property of the ImageList set to Depth24Bit or higher?
ColorDepth property must be set to Depth24Bit before you add image to ImageList. In my case it helped.

Categories