2009-07-02 43 views
8

我發現了許多來自Microsoft Office 2007的漂亮圖標。你有沒有想法提取&使用VBA將所有圖標保存爲PNG文件?如何從Microsoft Office 2007保存ImageMSO圖標?

Partial ImageMSO http://rabu4g.bay.livefilestore.com/y1p2SF1q63YjDjPNmK4nYMW2644r9AO2aAsE__vBYznTeXD0b4SJUU0O07fxPD0r7aO_83gCJ-8OfcOQsFKG0fQMRnTEneBU1TI/Capture.PNG

下面的代碼是用於從ImageMSO獲取圖像的代碼。

Application.CommandBars.GetImageMso([name], [width], [height]) 

我可以全部顯示爲PictureBox控件並將Excel文件保存爲網頁。但是,每個圖標質量都很低。

此外,我嘗試通過使用以下代碼創建C#Excel加載項項目作爲位圖對象導出。但我發現它無法導出爲半透明PNG。

stdole.IPictureDisp p = Application.CommandBars.GetImageMso(fileName, size, size); 
Bitmap b = Bitmap.FromHbitmap((IntPtr)p.Handle, (IntPtr)p.hPal); 

PS。我想將所有圖標保存爲PNG格式,因爲我需要使用它的半透明功能。它允許我使用大多數背景色的所有圖標而不是白色背景。

+0

@Soul_Master與提取任何運氣? – 2010-02-24 13:19:24

+0

不是。我只是放棄這個。 – 2010-02-24 18:49:16

回答

1

可以找到所有的PNG文件here這些都是PNG格式。好的編程! (一個漂亮的ZIP存檔也可用Here)ZIP存檔包含全部17個Excel圖標。

當您使用GetImageMso方法時,最終會得到對象的IPicture接口。 IPicture界面訪問適合以原始格式保存到文件的圖標 - .ICO,.WMF或.BMP不支持PNG格式。以下鏈接解釋爲什麼這是不能直接:

http://msdn.microsoft.com/en-us/library/aa434604.aspx(msoGetImageMso法) http://msdn.microsoft.com/en-us/library/ms680761%28VS.85%29.aspx(IPICTURE接口) http://msdn.microsoft.com/en-us/library/ms694504%28VS.85%29.aspx(另存爲文件的方法)

然而,使用更復雜的方法將產生你想要什麼:

http://blogs.msdn.com/mshneer/archive/2007/10/10/preserving-transparency-when-rendering-office-icons.aspx

4

我已經結束了一個C#實用工具類提取的Office2007畫廊圖標.png文件,同時保持透明度prope RLY。主要代碼摘自Andrew Whitechapel撰寫的一篇偉大文章( http://blogs.msdn.com/b/andreww/archive/2007/10/10/preserving-the-alpha-channel-when-converting-images.aspx)。如果您想將所有這些圖標提取到目標文件夾,我已將它與Office 2007示例圖標表集成在一起。

步驟是:

1)http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=11675

2下載Office庫電子表格)與Office2007IconsGallery.xlsm示例電子表格的位置調用OfficeIcons.ExtractAllIcons(),並在您想要的目標文件夾圖標被提取。

{}代碼

using System; 
using System.Drawing; 
using System.Drawing.Imaging; 
using System.IO; 
using System.Runtime.InteropServices; 
using System.Xml.Linq; 
using ExcelDna.Integration; 
using ICSharpCode.SharpZipLib.Zip; 
using Microsoft.Office.Interop.Excel; 
using stdole; 

public class OfficeIconUtils 
{ 
    public static void ExtractAllIcons(string xlsmPath, string targetFolder) 
    { 
     // extract customUI.xml 
     var zf = new ZipFile(xlsmPath); 
     var entry = zf.GetEntry("customUI/customUI.xml"); 
     var zipStream = zf.GetInputStream(entry); 
     XNamespace ns = "http://schemas.microsoft.com/office/2006/01/customui"; 
     var root = XElement.Load(zipStream); 
     foreach (var gallery in root.Descendants(ns + "gallery")) 
     { 
      //create a sub-folder for the gallery 
      var subFolder = Path.Combine(targetFolder, 
       gallery.Attribute("label").Value); 
      var width = int.Parse(gallery.Attribute("itemWidth").Value); 
      var height = int.Parse(gallery.Attribute("itemHeight").Value); 
      Directory.CreateDirectory(subFolder); 
      foreach (var item in gallery.Descendants(ns + "item")) 
      { 
       SaveIcon(item.Attribute("imageMso").Value, 
        subFolder, width, height); 
      } 
     } 
    } 

    public static void SaveIcon(string msoName, string folder, 
     int width = 32, int height = 32) 
    { 
     ConvertPixelByPixel(
      ((Application)(ExcelDnaUtil.Application)) 
       .CommandBars.GetImageMso(msoName, width, height)) 
      .Save(Path.Combine(folder, string.Format("{0}.png", 
      msoName)), ImageFormat.Png); 
    } 


    public static Bitmap ConvertPixelByPixel(IPictureDisp ipd) 
    { 
     // get the info about the HBITMAP inside the IPictureDisp 
     var dibsection = new DIBSECTION(); 
     GetObjectDIBSection((IntPtr)ipd.Handle, Marshal.SizeOf(dibsection), ref dibsection); 
     var width = dibsection.dsBm.bmWidth; 
     var height = dibsection.dsBm.bmHeight; 

     // create the destination Bitmap object 
     var bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb); 

     unsafe 
     { 
      // get a pointer to the raw bits 
      var pBits = (RGBQUAD*)(void*)dibsection.dsBm.bmBits; 

      // copy each pixel manually 
      for (var x = 0; x < dibsection.dsBmih.biWidth; x++) 
       for (var y = 0; y < dibsection.dsBmih.biHeight; y++) 
       { 
        var offset = y * dibsection.dsBmih.biWidth + x; 
        if (pBits[offset].rgbReserved != 0) 
        { 
         bitmap.SetPixel(x, y, Color.FromArgb(pBits[offset].rgbReserved, pBits[offset].rgbRed, pBits[offset].rgbGreen, pBits[offset].rgbBlue)); 
        } 
       } 
     } 

     return bitmap; 
    } 

    [StructLayout(LayoutKind.Sequential)] 
    private struct RGBQUAD 
    { 
     public byte rgbBlue; 
     public byte rgbGreen; 
     public byte rgbRed; 
     public byte rgbReserved; 
    } 

    [StructLayout(LayoutKind.Sequential)] 
    public struct BITMAP 
    { 
     public Int32 bmType; 
     public Int32 bmWidth; 
     public Int32 bmHeight; 
     public Int32 bmWidthBytes; 
     public Int16 bmPlanes; 
     public Int16 bmBitsPixel; 
     public IntPtr bmBits; 
    } 

    [StructLayout(LayoutKind.Sequential)] 
    public struct BITMAPINFOHEADER 
    { 
     public int biSize; 
     public int biWidth; 
     public int biHeight; 
     public Int16 biPlanes; 
     public Int16 biBitCount; 
     public int biCompression; 
     public int biSizeImage; 
     public int biXPelsPerMeter; 
     public int biYPelsPerMeter; 
     public int biClrUsed; 
     public int bitClrImportant; 
    } 

    [StructLayout(LayoutKind.Sequential)] 
    public struct DIBSECTION 
    { 
     public BITMAP dsBm; 
     public BITMAPINFOHEADER dsBmih; 
     public int dsBitField1; 
     public int dsBitField2; 
     public int dsBitField3; 
     public IntPtr dshSection; 
     public int dsOffset; 
    } 

    [DllImport("gdi32.dll", EntryPoint = "GetObject")] 
    public static extern int GetObjectDIBSection(IntPtr hObject, int nCount, ref DIBSECTION lpObject); 

} 

{}代碼

+0

如果您指定了依賴關係的500個依賴項,那就更好了,更不用說一旦找到它們,所有代碼都不會編譯。 – 2014-06-16 05:43:42

0

我已經試過伊斯梅爾的答案和它的工作很大。然而,我花了一段時間才弄清楚如何使它工作。我可能會分享這一點的知識:

該解決方案需要來自codeplex的ExcelDna:link

由於我使用Net 4.0我沒有.ZIP支持,所以我首先.xslm文件解壓縮到一個扁平的目錄結構,然後我改變了代碼直接從文件中讀取。 然後在Excel中我稱之爲ExcelDna擴展方法

=ExtractIcons("Office2207IconsGallery";"folder_where_to_store_icons") 

using語句的實用工具類(對我來說):

using System.Xml.Linq; 

using System.IO; 

using System.Drawing; 

using System.Runtime.InteropServices; 

using System.Drawing.Imaging; 

using Application = Microsoft.Office.Interop.Excel.Application; 

using ExcelDna.Integration; 

using stdole; 

希望這有助於....謝謝伊斯梅爾!

7

我在我的Excel開發中經常使用ImageMso。在這篇文章中我偶然發現了一些東西,並將它們放在一起,以直觀地搜索,提取並保存Microsoft Excel中的圖標作爲文件,或者複製並粘貼(使用Alpha通道透明)到另一個應用程序。我還編制了來自各種來源的8,899個不同的ImageMso名稱列表。我希望別人能找到這個有用的。

Microsoft Office Icons (ImageMSO) Gallery & Extraction

ImageMSO Gallery on Microsoft Excel 2013 running Windows 8

相關問題