最后译文在第3页第21楼,感谢nighmore朋友的全力帮助,还有Kyo斑竹。 斑竹我由于项目需要,要看以下这篇文章,请求朋友们帮忙翻译一下。 虽然出于我个人的理由需要大家帮忙,但是我保证这篇文章是一项非常有用的算法技术。 大家翻译后必定有很大帮助,至少比光看语法好得多。 我说一下翻译的要求,除代码和注释以外,全部都希望大家帮忙翻译, 我本人也懂英语,不过就没时间翻译全文,希望有心人帮忙,不用全部翻译, 就大家看得感兴趣的段落哪怕是三两句,看得懂的就帮忙翻译出来, 以跟帖的形式,大家几句几句地凑起来,就可以成为完整的译文, 我以斑竹的身份加上10000论坛币请求大家贡献一点点时间,就算只是翻译1~2句, 我也感激,也会加分,呵呵……谢谢! PS:我有空也会几句几句地翻译出来。 Adding and using 32 bit alphablended images and icons to the imagelist control By Narb M An article on adding alphablended images to an imagelist control and using them on components. Introduction As time goes on and applications evolve, the end user expects a richer and more vibrant user interface. Not only is it visually appealing, a nice user interface also serves as a function. Better design and use of icons to represent tasks will not only make your application stand out but will also give the end user a better experience working with your applications. This is the reason why I decided to publish this article. Now, let's jump to the technical aspects... We've all seen those nice toolbar icons Microsoft (c) uses in their products. They include shadows, 24 bit true colors, transparencies etc. These images are actually 24 bit images that contain an alpha channel which makes them 32 bit format. This format not only allows transparent pixels, but semi-transparent pixels as well. This allows images to blend in with their background nicely without the "jaggedness" you sometimes see with regular 24 bit transparent images. If you have ever tried using such icons or images in your applications, you have quickly found out that it's somewhat more of a challenge than you expected, and in fact there are some obstacles to overcome. Not to mention a bug that exists in the framework that will prevent you from displaying such images properly. In this article, I will try to explain what is needed to overcome these obstacles and also how to use my ImageList class to fix the problem. From this point on, I will refer to both icons and images as just images and also refer to 24 bit alpha blended images as just 32 bit images. The Problem The bug I mentioned above, I believe, exists in the ImageList component itself. If you try to add a 32 bit image using the Images.Add() method of the ImageList control, or try to add images using the Visual Studio IDE, you will find that the alpha channel becomes either corrupt or lost all together based on the raw format of the image file itself. An example of this would be to create a new Bitmap object and load a 32 bit image such as a .png file, using the Bitmap constructor like: Bitmap bm = new Bitmap( file );, and then using the ImageList. Add method to add the bitmap object to the ImageList. Even though the bitmap's .IsAlphaFormat property is true, the image becomes corrupt in the process of adding to the ImageList control, and if you try to use these images on controls, you will find that they are displayed incorrectly. My belief is that the problem exists in the way the .Add() method extracts the alpha channel and copies it to the ImageList from a bitmap structure. The reason why I came to this conclusion was because supplying a handle to an image with the .Add() method seems to work. You will see I use this method for Icons (.ico files) since the Icon class has a .FromHandle method that returns a Windows icon. Now, if you try using these added images for your controls, you will find that since the alpha channel has become corrupt, the images contain solid black for the areas where alpha blending was to take place. In cases where you add PNG files with alpha channel, you will find that alpha blending does occur but contains a subtle color error in the areas where alpha blending was to take place. You can see a good example of this in the included image above by looking at the zoomed IE and MSN icon on the left. This might not be an eye catching thing for these 32x32 pixel icons but when you have 16x16 icons, the color error is very apparent. In conclusion, the ImageList control itself can hold 32 bit alpha blended images with no problems but the methods used to add those images are causing the corruption to occur. The Fix I have come up with a simple class with some static methods to perform the adding of 32 bit images to your ImageLists. The class Imagelist and its five methods allow you to add images correctly from various sources. These sources include an Image or Icon object, an external image file or icon file, an embedded image resource in your project, any external file icon or folder icon, and any file system extension icon. Each of these have examples and explanations below. Various methods are used for adding images from the different sources, but for the main part, Win32 API is used. When adding images from external files or from an Image object, basically what is done is a new bitmap is created using the CreateDIBSection() API call which resides in the gdi32.dll, and creates a bitmap object and returns a handle to the bitmap object in memory. Then the image is copied into this area in memory with correct formatting. The bitmap is then loaded into the ImageList using the ImageList_Add() call which takes a handle to an ImageList control and a handle to a bitmap object. When adding Icon objects or files, you can add them correctly by using the Icon object's .Handle property. Once the images are loaded into the ImageList control, you can use them normally for toolbars, listviews, etc. by assigning the indexer to a control. Also, all of its native methods hold and return correct values such as the .Count property. Before using the code First and foremost, your C# application and control properties must be setup correctly before the images are displayed properly. Let's go over these first before moving onto the usage of the class. You will see below that the Application.EnableVisualStyles() and Application.DoEvents() must be made before a .Run method. Another crucial property that must be set is the ImageList ColorDepth property, which must be set to ColorDepth.Depth32Bit. You can of course set this property using the Visual Studio IDE. Also keep in mind that alpha blended images are only supported in Windows XP+ and .NET Framework 1.1+. This code will have no effect on earlier versions of Windows. // // Enabling Visual Styles // static void Main() { // These two calls MUST be made before the // .Run method to enable 32bit icons to be used. Application.EnableVisualStyles(); Application.DoEvents(); // The color depth of the ImageList MUST be set // to 32bit before images can be added to the control. myImagelist.ColorDepth = ColorDepth.Depth32Bit; Application.Run(new Form1()); } Public Methods of the ImageList Class public sealed class Imagelist { public static void AddFromImage(Image sourceImage, ImageList destinationImagelist) {..} public static void AddFromImage(Icon sourceIcon, ImageList destinationImagelist) {..} public static void AddFromFile(string fileName, ImageList destinationImagelist) {..} public static void AddIconOfFile(string fileName, IconSize iconSize, bool selectedState, bool openState, bool linkOverlay, ImageList destinationImagelist) {..} public static void AddIconOfFile(string fileName, ImageList destinationImagelist) {..} public static void AddIconOfFileExt(string fileExtension, IconSize iconSize, bool selectedState, bool openState, bool linkOverlay , ImageList destinationImagelist) {..} public static void AddIconOfFileExt(string fileExtension, ImageList destinationImagelist) {..} public static void AddEmbeddedResource(string resourceName, ImageList destinationImagelist) {..} }
[此贴子已经被作者于2005-4-16 18:20:59编辑过]