如何在Windows Mobile下使用.NET Compact Framework從執行檔案取出Icon

來源:互聯網
上載者:User
需求

需要把其他執行檔案的ICON讀取出來,然後在程式中顯示。

 

實現
class ExtractIcon
{
public static Bitmap GetBitmapFromExeIcon(string path)
{
return GetBitmap(GetIconFromExe(path));
}

public static Icon GetIconFromExe(string path)
{
return GetIconFromExe(path, true);
}

//http://www.pinvoke.net/default.aspx/shell32/ExtractIconEx.html
public static Icon GetIconFromExe(string path, bool large)
{
IntPtr hLargeIcon = IntPtr.Zero;
IntPtr hSmallIcon = IntPtr.Zero;
ExtractIconEx(path, 0, ref hLargeIcon, ref hSmallIcon, 1);
if (large)
{
return (Icon)Icon.FromHandle(hLargeIcon).Clone();
}
else
{
return (Icon)Icon.FromHandle(hSmallIcon).Clone();
}
}

//http://social.msdn.microsoft.com/Forums/en-US/netfxcompact/thread/e765775c-a5b5-493f-baeb-3eaee1d41cef
public static Bitmap GetBitmap(Icon icon)
{
Bitmap bmp = new Bitmap(icon.Width, icon.Height);

//Create temporary graphics
Graphics gxMem = Graphics.FromImage(bmp);

//Draw the icon
gxMem.DrawIcon(icon, 0, 0);

//Clean up
gxMem.Dispose();

return bmp;
}

//http://msdn.microsoft.com/en-us/library/aa922154.aspx
[DllImport("coredll.dll", SetLastError = true)]
private static extern IntPtr ExtractIconEx(string fileName, int index, ref IntPtr hIconLarge, ref IntPtr hIconSmall, uint nIcons);
}

MS提供了ExtractIconEx函數可以取出執行檔案的大小Ico表徵圖資訊。第三個參數為大表徵圖,第四個參數為小表徵圖。

由於Icon對象不能直接在Graphics上畫,所以需要轉換成Bitmap,但是Compact Framework又不支援Icon.Save(),所以實現了一個Bitmap GetBitmap(Icon icon) 函數。

如下,取出word的表徵圖。

第二個圖去掉ico背景

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.