如何从 Microsoft Office 2007 保存 ImageMSO 图标?

发布于 2024-07-25 13:24:33 字数 927 浏览 4 评论 0原文

我从 Microsoft Office 2007 中发现了很多漂亮的图标。您对提取和编辑有什么想法吗? 使用VBA将所有图标保存为PNG文件?

部分图像MSO 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);

附言。 我想将所有图标保存为 PNG 格式,因为我需要使用它的半透明功能。 它允许我在大多数背景颜色上使用所有图标,而不是白色背景。

I found a lot of nice icons from Microsoft Office 2007. Do you any idea for extract & save all icons as PNG files by using VBA?

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

The following code is code which is used to get image from ImageMSO.

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

I can display all as PictureBox control and save excel file as web page. However, every icons is very low quality.

Moreover, I try to create C# Excel Add-in project for exporting as Bitmap object by using the following code. But I found that it can't export as semi-transparent PNG.

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

PS. I want to save all icons as PNG format because I need to use semi-transparent feature of it. It allow me to use all icons on most background color more than white background.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

柠檬心 2024-08-01 13:24:33

我在 Excel 开发中经常使用 ImageMso。 偶然发现这篇文章后,我更进一步,将其放在一起,将 Microsoft Excel 中的图标进行可视化搜索、提取和保存为文件,或复制并粘贴(具有 alpha 通道透明度)到另一个应用程序。 我还从各种来源编制了 8,899 个不同 ImageMso 名称的列表。 我希望其他人会发现这很有用。

Microsoft Office 图标 (ImageMSO) 库和图标 提取

ImageMSO运行 Windows 8 的 Microsoft Excel 2013 上的图库

I use ImageMso quite frequently in my Excel development. Having stumbled on this post, I took it a step further and put a package together to visually search, extract and save icons from Microsoft Excel as a file or copy and paste (with alpha channel transparency) to another application. I also compiled a list of 8,899 distinct ImageMso names from the various sources. I hope others can find this useful.

Microsoft Office Icons (ImageMSO) Gallery & Extraction

ImageMSO Gallery on Microsoft Excel 2013 running Windows 8

难得心□动 2024-08-01 13:24:33

我已经封装了一个 C# 实用程序类,用于将 Office2007 库图标提取到 .png 文件,同时正确保持其透明度。 主要代码取自 Andrew Whitechapel 写的一篇很棒的文章(
http ://blogs.msdn.com/b/andreww/archive/2007/10/10/preserving-the-alpha-channel-when-converting-images.aspx)。 我已将其与 Office 2007 示例图标表集成,以防您要将所有这些图标提取到目标文件夹。

步骤如下:

1) 从 http: 下载 Office Gallery 电子表格: //www.microsoft.com/download/en/details.aspx?displaylang=en&id=11675

2) 使用 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);

}

{代码}

I've wrapped up a C# Utility class for extracting Office2007 gallery icons to .png files, while maintaining their transparency properly. The main code is taken from a great article written by Andrew Whitechapel (
http://blogs.msdn.com/b/andreww/archive/2007/10/10/preserving-the-alpha-channel-when-converting-images.aspx). I've integrated this with the Office 2007 sample icon sheet, in case you want to extract all of these icons to a target folder.

Steps are:

1) Download the Office Gallery spreadsheet at http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=11675

2) Call OfficeIcons.ExtractAllIcons() with the location of the Office2007IconsGallery.xlsm sample spreadsheet, and the target folder where you want the icons extracted.

{code}

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);

}

{code}

怀中猫帐中妖 2024-08-01 13:24:33

所有 PNG 文件均可在此处 这些都已经是 PNG 格式了。 很好的编程! (还有一个不错的 ZIP 存档 此处)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

All of the PNG files can be found here These are all in PNG format already. Good programming! (a nice ZIP archive is also available Here) The ZIP archive contains all 17 of the Excel icons.

When you use the GetImageMso method, you end up with an IPicture interface to the object. The IPicture interface accesses the icon suitable for saving to a file in the original format - an .ICO, .WMF or a .BMP The PNG format is not supported. The following links explain why this is not directly possible:

http://msdn.microsoft.com/en-us/library/aa434604.aspx (msoGetImageMso method)
http://msdn.microsoft.com/en-us/library/ms680761%28VS.85%29.aspx (IPicture Interface)
http://msdn.microsoft.com/en-us/library/ms694504%28VS.85%29.aspx (Save As File method)

However, using a more complex approach will yield what you want:

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

伪心 2024-08-01 13:24:33

我尝试过伊斯梅尔的答案,效果很好。 然而我花了一段时间才弄清楚如何让它发挥作用。 我可以分享一点知识:

该解决方案需要来自 codeplex 的 ExcelDna:链接

由于我使用的是 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;

希望这有帮助......谢谢 Ismail!

I have tried Ismail's answer and it did work great. However it took me a while to figure out how to make it work. I may share this bit of knowledge:

The solution requires ExcelDna from codeplex: link.

As I am using Net 4.0 I do not have .zip support so I first extracted the .xslm files to a flat directory structure then I changed the code to read directly from the files.
Then in the Excel I call the ExcelDna extension method as

=ExtractIcons("Office2207IconsGallery";"folder_where_to_store_icons")

The using statements for the utility class (for me):

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;

Hope this helps.... Thank you Ismail!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文