使用shell导入缩览图时,读取到的关于受损文件的图片有黑色边缘,因此诞生了以下方法,但效率比使用shell高3 ~ 4倍(耗时主要是将黑色背景转换为透明的过程)。
1.添加类WindowsThumbnailProvider
[csharp] view plain copy
- [Flags]
- public enum ThumbnailOptions
- {
- None = 0x00,
- BiggerSizeOk = 0x01,
- InMemoryOnly = 0x02,
- IconOnly = 0x04,
- ThumbnailOnly = 0x08,
- InCacheOnly = 0x10,
- }
- public class WindowsThumbnailProvider
- {
- private const string IShellItem2Guid = "7E9FB0D3-919F-4307-AB2E-9B1860310C93";
- [dllImport(";, CharSet = C, SetLastError = true)]
- internal static extern int SHCreateItemFromParsingName(
- [MarshalA)] string path,
- // The following parameter is not used - binding context.
- IntPtr pbc,
- ref Guid riid,
- [MarshalA)] out IShellItem shellItem);
- [DllImport("gdi32.dll")]
- [return: MarshalA)]
- internal static extern bool DeleteObject(IntPtr hObject);
- [ComImport]
- [InterfaceType)]
- [Guid("43826d1e-e718-42ee-bc55-a1e261c37bfe")]
- internal interface IShellItem
- {
- void BindToHandler(IntPtr pbc,
- [MarshalA)]Guid bhid,
- [MarshalA)]Guid riid,
- out IntPtr ppv);
- void GetParent(out IShellItem ppsi);
- void GetDisplayName(SIGDN sigdnName, out IntPtr ppszName);
- void GetAttributes(uint sfgaoMask, out uint psfgaoAttribs);
- void Compare(IShellItem psi, uint hint, out int piOrder);
- };
- internal enum SIGDN : uint
- {
- NORMALDISPLAY = 0,
- PARENTRELATIVEPARSING = 0x80018001,
- PARENTRELATIVEFORADDRESSBAR = 0x8001c001,
- DESKTOPABSOLUTEPARSING = 0x80028000,
- PARENTRELATIVEEDITING = 0x80031001,
- DESKTOPABSOLUTEEDITING = 0x8004c000,
- FILESYSPATH = 0x80058000,
- URL = 0x80068000
- }
- internal enum HResult
- {
- Ok = 0x0000,
- False = 0x0001,
- InvalidArguments = unchecked((int)0x80070057),
- OutOfMemory = unchecked((int)0x8007000E),
- NoInterface = unchecked((int)0x80004002),
- Fail = unchecked((int)0x80004005),
- ElementNotFound = unchecked((int)0x80070490),
- TypeElementNotFound = unchecked((int)0x8002802B),
- NoObject = unchecked((int)0x800401E5),
- Win32ErrorCanceled = 1223,
- Canceled = unchecked((int)0x800704C7),
- ResourceInUse = unchecked((int)0x800700AA),
- AccessDenied = unchecked((int)0x80030005)
- }
- [ComImportAttribute()]
- [GuidAttribute("bcc18b79-ba16-442f-80c4-8a59c30c463b")]
- [InterfaceTypeAttribute)]
- internal interface IShellItemImageFactory
- {
- [PreserveSig]
- HResult GetImage(
- [In, MarshalA)] NativeSize size,
- [In] ThumbnailOptions flags,
- [Out] out IntPtr phbm);
- }
- [StructLayou)]
- internal struct NativeSize
- {
- private int width;
- private int height;
- public int Width { set { width = value; } }
- public int Height { set { height = value; } }
- };
- [StructLayou)]
- public struct RGBQUAD
- {
- public byte rgbBlue;
- public byte rgbGreen;
- public byte rgbRed;
- public byte rgbReserved;
- }
- public static Bitmap GetThumbnail(string fileName, int width, int height, ThumbnailOptions options)
- {
- IntPtr hBitmap = GetHBitma(fileName), width, height, options);
- try
- {
- // return a Sy from the hBitmap
- return GetBitmapFromHBitmap(hBitmap);
- }
- finally
- {
- // delete HBitmap to avoid memory leaks
- DeleteObject(hBitmap);
- }
- }
- public static Bitmap GetBitmapFromHBitmap(IntPtr nativeHBitmap)
- {
- Bitmap bmp = Bi(nativeHBitmap);
- if ) < 32)
- return bmp;
- return CreateAlphaBitmap(bmp, PixelFormat.Format32bppArgb);
- }
- public static Bitmap CreateAlphaBitmap(Bitmap srcBitmap, PixelFormat targetPixelFormat)
- {
- Bitmap result = new Bitma, , targetPixelFormat);
- Rectangle bmpBounds = new Rectangle(0, 0, , );
- BitmapData srcData = (bmpBounds, ImageLockMode.ReadOnly, );
- bool isAlplaBitmap = false;
- try
- {
- for (int y = 0; y <= - 1; y++)
- {
- for (int x = 0; x <= - 1; x++)
- {
- Color pixelColor = Color.FromArgb(
- Mar, * y) + (4 * x)));
- if > 0 & < 255)
- {
- isAlplaBitmap = true;
- }
- re(x, y, pixelColor);
- }
- }
- }
- finally
- {
- (srcData);
- }
- if (isAlplaBitmap)
- {
- return result;
- }
- else
- {
- return srcBitmap;
- }
- }
- private static IntPtr GetHBitmap(string fileName, int width, int height, ThumbnailOptions options)
- {
- IShellItem nativeShellItem;
- Guid shellItem2Guid = new Guid(IShellItem2Guid);
- int retCode = SHCreateItemFromParsingName(fileName, In, ref shellItem2Guid, out nativeShellItem);
- if (retCode != 0)
- throw Mar(retCode);
- NativeSize nativeSize = new NativeSize();
- na = width;
- na = height;
- IntPtr hBitmap;
- HResult hr = ((IShellItemImageFactory)nativeShellItem).GetImage(nativeSize, options, out hBitmap);
- Mar(nativeShellItem);
- if (hr == HRe) return hBitmap;
- throw Mar((int)hr);
- }
- }
2.使用方法:[csharp] view plain copy
- int pic_size = 256;
- Bitmap bm = Window(path, pic_size, pic_size, T);
1.《获取c语言随机文件的缩略图》援引自互联网,旨在传递更多网络信息知识,仅代表作者本人观点,与本网站无关,侵删请联系页脚下方联系方式。
2.《获取c语言随机文件的缩略图》仅供读者参考,本网站未对该内容进行证实,对其原创性、真实性、完整性、及时性不作任何保证。
3.文章转载时请保留本站内容来源地址,https://www.lu-xu.com/keji/1956562.html