C#: Using .ico file with multiple images - c#

I'm trying to set the images in a TreeView in C#, from a .ico file containing two icons: a 32x32 version and a 16x16 version. The images are setting, but .Net is choosing the 32x32 version, and scaling it down (which looks terrible) instead of choosing the readily available 16x16 image.
The relevant code:
ilTree.Images.Add(Properties.Resources.group);
ilTree.Images.Add(Properties.Resources.single);
ilTree.Images.Add(Properties.Resources.db);
treeStored.ImageList = ilTree;
Where am I going wrong?
Answer:
There are two things you must do to get this to work. The first, as mentioned below, is to manually specify the correct size to the image list. The second is that you will also probably have to specify the color depth. MSDN states that:
In the .NET Framework version 1.1 or later, the default is Depth8Bit.
...however, that did not prevent ImageList from removing colors from my 8-bit icon. Upon close inspection, my icons (there were three) were: 4bit, 4bit, 8bit. The two 4 bit icons shared palettes, however the 8bit had a different one. In total, there were 257 colors between the icons. Despite the only slight overflow, .Net knocked it down to a mere 20 colors.

You need to create a new Icon object. There is an overload for the constructor that accepts the original Icon object and a size. I don't know if the new Icon object will share the same HIcon, but it will draw properly. To be safe, I would recommend ensuring that both are disposed.

Set the ImageSize property of the ImageList to new Size(16, 16)

Related

How to create an image with multiple tiles in the background?

I would like to know how I can create one image from many. I would like to create a tile in my windows phone application like in this image (specifically, the People tile):
(source: addictivetips.com)
I have nine pictures, and I would create an image, that I will add like tile to background. Does anybody know how can I create an image that looks like the one in that picture?
I have very little experience in this space, but have you considered creating a control that simply displays up to 9 pictures side by side in a grid like that? You then can bind each image independently & change them out however you want. This article touches on how to bind phontos in WP7 nicely:
http://msdn.microsoft.com/en-us/library/hh286418(v=vs.92).aspx
If you're talking about assembling an actual graphic image like a jpeg or bitmap, you'll need to look at the Image Class, Bitmap Class, and Graphics Class. Essentially you'll need to implement the following steps:
Load the relevant images with From method in Image, typically Image.FromFile.
Determine how many rows and columns you'll be using.
Calculate the total width and height for your layout using the widths and heights of the loaded images with appropriate padding added.
Create a new Bitmap of the appropriate size with the correct background color and iamge format.
Have variables for the current drawing location (x & y).
Have variables for the current row and column in your layout.
In a loop, Create your Graphics object.
Use Graphics.DrawImage to add your loaded image to the layout bitmap.
Increment your drawing row and or column as appropriate.
Calculate your new drawing location.
Repeat until done.
One of the options is to use WriteableBitmapEx
Also you can probably find an answer to your question here: How can I merge two images into one?

Image taken from ImageList looks different than taken straight from resource

I have a form with two buttons.
To one of them I assigned an image (a 16 x 16, 32 bit depth png) by setting the Image property from VS's properties editor (using the Import... button).
I also have an ImageList (16 x 16 ImageSize and Depth32Bit ColorDepth) to which I assigned the same image as to the first button also from the properties editor -> Images and then Add. Then I assigned this image to my second button this way:
button2.Image = imageList.Images[0];
And this is how the images look (2x the actual size):
Is it possible to have my second button look like my first one by using an ImageList? The reason why I use an ImageList is because when checking performance, the line of code that loaded the image from the resource was a hot spot according to VS's Performance Wizard.
My application will have a list of controls, each of which have a bunch of buttons with images, so I want them to load as fast as possible. So what I have is a static ImageList from which each of these controls get their images.
This is because the images you have added in your Imagelist have higher ColorDepth that the ColorDepth you have set in your ImageList Properties. for example, if the images you have added are in PNG format, you should set the ColorDepth of you ImageList (In the properties window) to Depth32Bit as the PNG format uses 32Bit color depth.

Image comparison - rotation, alignment and scaling

I have the following needs. There are some number of forms, i.e blanks - for example the ones used in surveys. The ones which aren't filled with information, I will call image templates from now on. Apart from the image templates, I have also many images, which are essentially the image templates filled with information. For example, there is a survey and there are two blanks for filling - these are the image templates. Many people have filled the blanks with their personal information and these are the images.
The image templates are scanned in a perfect shape. But many of the scanned images are tilted or not properly aligned, or maybe scaled. So I have the following requirement - every image must be recognized to which image template it belongs. After it is recognized, it must be properly skewed, aligned and scaled to the image template.
I know this is a complex task and that's why I need a library, preferably a C# one. I have found AForge, but till now I have only seen a suitable method for skewing. Essentially I need a library which takes as input an image template and an image, and sets a flag if the image does not match to the image template. But if it matchs it must return the appropriate skew angle, alignment and scaling.
If you have any ideas or used such a library, I will appreciate it greatly.
Wish you all the best,
Petar
The problem seems to be an image registration problem coupled with some template matching problem.
image registration
Depending on how the scanned document may be distorted (scale factor, rotation, skew...) one can register images using simple rigid transform (i.e. translation + rotation, only two corresponding points are needed) to more complex one such as non rigid transform (more corresponding point are needed). The corresponding points can be manually given but ideally should be automatically detected.
ITK library includes several methods for image registration
template matching problem
Once your images are aligned, the comparison between an image and possible templates database could be achieved by first extracting characteristic features in the image and comparing them to your template database. This is very general and should be refined with respect to the image used.
There is other way that combine both image registration and template matching
the Bag Of Features approach which consists in extracting interesting points (robust to several types of image deformation) from the image, the points generate a signature that characterizes the image, the image comparison being in fact a signature comparison.
I used to work for a company, Accusoft Pegasus, which has some interesting forms recognition software. I've not seen their FormFix tool in action in a few years, but it should be able to do what you are needing.

How can you draw 32bppargb images with the Win32 AlphaBlend function?

I've been fussing with this for the better part of the night, so maybe one of you can give me a hand.
I have found GDI+ DrawImage in C# to be far too slow for what I'm trying to render, and from the looks of forums, it's the same for other people as well.
I decided I would try using AlphaBlend or BitBlt from the Win32 API to get better performance. Anyway, I've gotten my images to display just fine except for one small detail—no matter what image format I use, I can't get the white background to disappear from my (transparent) graphics.
I've tried BMP and PNG formats so far, and verified that they get loaded as 32bppargb images in C#.
Here's the call I'm making:
// Draw the tile to the screen.
Win32GraphicsInterop.AlphaBlend(graphicsCanvas, destination.X, destination.Y, this.TileSize, this.TileSize,
this.imageGraphicsPointer, tile.UpperLeftCorner.X, tile.UpperLeftCorner.Y,
this.TileSize, this.TileSize,
new Win32GraphicsInterop.BLENDFUNCTION(Win32GraphicsInterop.AC_SRC_OVER, 0,
Convert.ToByte(opacity * 255),
Win32GraphicsInterop.AC_SRC_ALPHA));
For the record, AC_SRC_OVER is 0x00 and AC_SRC_ALPHA is 0x01 which is consistent with what MSDN says they ought to be.
Do any of you guys have a good solution to this problem or know a better (but still fast) way I can do this?
Graphics.DrawImage() speed is critically dependent on the pixel format. Format32bppPArgb is 10 times faster than any other one on any recent machine I've tried.
Also make sure you the image doesn't get resized, be sure to use a DrawImage() overload that sets the destination size equal to the bitmap size. Very important if the video adapter's DPI setting doesn't match the resolution of the bitmap.
Have you tried an opacity of just 255 rather than a calculated one?
This blog post describes what you're trying to do:-
http://blogs.msdn.com/andreww/archive/2007/10/10/preserving-the-alpha-channel-when-converting-images.aspx
Critical thing is that he carries out a conversion of the image to make the alpha channel compatible..
Ok. From a pure Win32 perspective:
In order for AlphaBlend to actually alpha blend... it needs the source device context to contain a selected HBITMAP representing an image with 32bpp bitmap with a pre-multiplied alpha channel.
To get a device bitmap with 32bpp you can either call one of the many functions that will create a screen compatible device bitmap and hope like hell the user has selected 32bpp as the desktop bitdepth. OR, ensure that the source bitmap is a DIBSection. Well, the library or framework that is creating it from the loaded image for you.
So, C# is loading your images with 32bpp argb, BUT, how are you converting that C# representation of the bitmap into a HBITMAP? You need to ensure that a DIB Section is being created, not a DDB (or device dependent bitmap), and that the DIB Section is 32bpp.

Listview icons show up blurry (C#)

I'm attempting to display a "LargeIcon" view in a listview control, however the images I specify are blurry. This is what I have so far:
alt text http://img220.imageshack.us/img220/1005/blurryiconsql3.jpg
The .png files are 48x48 and that's what I have it set to display at in the ImageList properties. There's one thing that I've noticed (which is probably the cause) but I don't know how to change it. Inside the "Images Collection Editor" where you choose what images you want for the ImageList control, it looks like it's setting the wrong size for each image.
alt text http://img83.imageshack.us/img83/5218/imagepropertiesmf9.jpg
As you can see the "PhysicalDimension" and the "Size" is set to 16x16 and not abled to be manipulated. Does anyone have any ideas? Many thanks!
Be sure to set ImageList.ImageSize to 48 x 48 too.
When adding a .PNG Icon format size the editor tends to pick the first entry size in that file, so it picks up the 16x16 entry and it's stretching that out. That's why you see the 16x16 in the properties there. As suggested, the support for PNG is poor, I'm often found myself rolling over to another format as well to avoid this.
You can open the file in Paint.Net if you need a free editor or something more fully featured like Photoshop or Fireworks and extract the exact size you want.
I'm not sure if its the problem in this specific case, but Microsoft support for the PNG format is generally poor. Try adding the images in .bmp format and they should display fine.
Check also the ColorDepth setting on your ImageList. I had a similar issue with a TreeView control, but after reading the previous posting regarding the size I found this setting, played around with it a bit and found that it greatly affects the way images from an ImageList are rendered. The higher the depth the better the quality.
Be sure to set the ImageList size to 48x48 px BEFORE you add the images.
If the ImageList is set to 32x32 and you add a 48x48 image, the icon is resized to 32x32. When you change the ImageList to 48x48 afterwards, the image is just resized again, thus losing quality and going blurry.
Also, Paint.NET (or Photoshop) can't open .ico files.
Visual Studio/.NET can handle 32-bit PNG images fine, the built-in image editor in VS is a bit lack-lustre though.

Categories

Resources