Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 如何从Microsoft Office 2007保存ImageMSO图标?_Vba_Icons_Office 2007 - Fatal编程技术网

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

Vba 如何从Microsoft Office 2007保存ImageMSO图标?,vba,icons,office-2007,Vba,Icons,Office 2007,我从微软Office2007中找到了很多不错的图标。你有没有想过使用VBA将所有图标提取并保存为PNG文件 以下代码是用于从ImageMSO获取图像的代码 Application.CommandBars.GetImageMso([name], [width], [height]) 我可以显示为PictureBox控件,并将excel文件保存为网页。然而,每个图标的质量都很低 此外,我尝试使用以下代码创建C#Excel加载项项目,以导出为位图对象。但我发现它不能作为半透明PNG导出 stdol

我从微软Office2007中找到了很多不错的图标。你有没有想过使用VBA将所有图标提取并保存为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格式的所有图标,因为我需要使用它的半透明功能。它允许我在大多数背景颜色上使用所有图标,而不是白色背景。

可以找到所有的PNG文件,这些文件都是PNG格式的。很好的编程!(还有一个漂亮的ZIP存档)ZIP存档包含所有17个Excel图标

使用GetImageMso方法时,最终会得到对象的IPacture接口。IPicture接口访问适合以原始格式保存到文件的图标-an.ICO、.WMF或.BMP不支持PNG格式。以下链接解释了为什么不能直接实现这一点:

(msogetimageso方法) (i结构接口) (另存为文件方法)

但是,使用更复杂的方法将产生您想要的结果:

我已经打包了一个C#实用程序类,用于将Office2007 gallery图标提取到.png文件中,同时适当地保持其透明度。主要代码取自Andrew Whitechapel撰写的一篇伟大的文章( )。我已将其与Office 2007示例图标表集成,以防您希望将所有这些图标提取到目标文件夹中

步骤如下:

1) 下载Office Gallery电子表格,网址为

2) 使用Office2007IconsGallery.xlsm示例电子表格的位置以及要提取图标的目标文件夹调用OfficeIcons.ExtractAllIcons()

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

}
使用系统;
使用系统图;
使用系统、绘图、成像;
使用System.IO;
使用System.Runtime.InteropServices;
使用System.Xml.Linq;
利用ExcelDna进行整合;
使用ICSharpCode.SharpZipLib.Zip;
使用Microsoft.Office.Interop.Excel;
使用stdole;
公共类officeutils
{
公共静态void ExtractAllIcons(字符串xlsmPath、字符串targetFolder)
{
//提取customUI.xml
var zf=新ZipFile(xlsmPath);
var entry=zf.GetEntry(“customUI/customUI.xml”);
var zipStream=zf.GetInputStream(条目);
XNS=”http://schemas.microsoft.com/office/2006/01/customui";
var root=XElement.Load(zipStream);
foreach(根目录下的变量库(ns+“库”))
{
//为库创建子文件夹
var subFolder=Path.Combine(targetFolder,
画廊。属性(“标签”)。价值);
var width=int.Parse(gallery.Attribute(“itemWidth”).Value);
var height=int.Parse(gallery.Attribute(“itemHeight”).Value);
Directory.CreateDirectory(子文件夹);
foreach(gallery.substands中的变量项(ns+“项”))
{
SaveIcon(item.Attribute(“imageMso”).Value,
子文件夹、宽度、高度);
}
}
}
公共静态无效保存图标(字符串msoName、字符串文件夹、,
整数宽度=32,整数高度=32)
{
逐像素转换(
((应用)(ExcelDnaUtil.应用))
.CommandBars.GetImageMso(msoName、宽度、高度))
.Save(Path.Combine(文件夹,string.Format(“{0}.png”),
msoName),ImageFormat.Png);
}
公共静态位图转换PixelbyPixel(IPictureDisp ipd)
{
//获取IPictureDisp中有关HBITMAP的信息
var dibsection=新的dibsection();
GetObjectDIBSection((IntPtr)ipd.Handle,Marshal.SizeOf(dibsection),ref dibsection);
var宽度=dibsection.dsBm.bmWidth;
var高度=dibsection.dsBm.bmHeight;
//创建目标位图对象
var bitmap=新位图(宽度、高度、像素格式.Format32bppArgb);
不安全的
{
//获取指向原始位的指针
var pBits=(RGBQUAD*)(void*)dibsection.dsBm.bmBits;
//手动复制每个像素
对于(var x=0;x=ExtractIcons("Office2207IconsGallery";"folder_where_to_store_icons")
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;