.net Gimp gdk_pixbuf_save失败,AccessViolationException异常,但我没有';我不知道为什么?

.net Gimp gdk_pixbuf_save失败,AccessViolationException异常,但我没有';我不知道为什么?,.net,64-bit,pdf-generation,access-violation,gimp,.net,64 Bit,Pdf Generation,Access Violation,Gimp,我正在使用我找到的代码将SVG文件光栅化为图像文件。链接是。我的代码看起来一模一样,但我的访问违反了: “试图读取或写入受保护的内存。这通常表示其他内存已损坏。”` 为了消除显而易见的问题:我使用Gimp目录中的svg文件,因此我有一个已知的好文件。我使用e:作为保存到文件的位置,这样库就不会遇到任何安全/权限问题。我正在以管理员权限运行Visual Studio,以防出现问题。我的框是64位的,我将Local.testsettings设置为“在64位机器上运行64位进程中的测试” 有什么想法吗

我正在使用我找到的代码将SVG文件光栅化为图像文件。链接是。我的代码看起来一模一样,但我的访问违反了:

“试图读取或写入受保护的内存。这通常表示其他内存已损坏。”`

为了消除显而易见的问题:我使用Gimp目录中的svg文件,因此我有一个已知的好文件。我使用e:作为保存到文件的位置,这样库就不会遇到任何安全/权限问题。我正在以管理员权限运行Visual Studio,以防出现问题。我的框是64位的,我将Local.testsettings设置为“在64位机器上运行64位进程中的测试”

有什么想法吗

我的代码是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.ComponentModel;

namespace TA.Rasterizer
{
    /// <summary>
    /// Takes SVG image file type and converts to PNG image file type. 
    /// Gimp must be installed. Gimp dll location must be in the web.config
    /// file key "ThreatAnalyzer-DllDirectoryForGimp".
    /// <summary>
    /// http://msdn.microsoft.com/en-us/magazine/cc164123.aspx
    /// http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_24547525.html
    /// </summary>    /// </summary>
    public static class Rasterizer
    {
        // C:\Windows\System32
        [DllImport("kernel32.dll", SetLastError = true)]
        static extern bool SetDllDirectory(string pathname);

        [DllImport("libgobject-2.0-0.dll", SetLastError = true)]
        static extern void g_type_init();

        [DllImport("librsvg-2-2.dll", SetLastError = true)]
        static extern IntPtr rsvg_pixbuf_from_file_at_size(string file_name, int width, int height, out IntPtr error);

        [DllImport("libgdk_pixbuf-2.0-0.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Auto)]
        static extern bool gdk_pixbuf_save(IntPtr pixbuf, string filename, string type, out IntPtr error, __arglist);
        //static extern bool gdk_pixbuf_save_to_buffer(

        /// <summary>
        /// Get location of Gimp installed Dlls from web.config appsettings key/value.
        /// Key name is found in this.webConfigFileAppSettingsGimp
        /// </summary>
        /// <returns></returns>
        private static string GetGimpDllLocationFromConfigurationFile(bool webapp, string nonDefaultConfigFileName)
        {
            // connect to config file
            Configuration rasterizerConfiguration = new TA.Rasterizer.Configuration(webapp, nonDefaultConfigFileName);

            // grab config setting
            return rasterizerConfiguration.Get("ThreatAnalyzer-DllDirectoryForGimp");
        }

        /// <summary>
        /// http://msdn.microsoft.com/en-us/magazine/cc164123.aspx
        /// You must check that file exists after call in order to check success. 
        /// </summary>
        /// <param name="inputFileName">SVG file to be converted</param>
        /// <param name="outputFileName">PNG file as final product</param>
        /// <returns></returns>
        public static void SvgToPng(string inputFileName, string outputFileName, bool webapp, string nonDefaultConfigFileName)
        {
            string gimpDllLocation = GetGimpDllLocationFromConfigurationFile(webapp, nonDefaultConfigFileName);

            bool rasterizeSuccess = ExternRaster.RasterizeSvg(inputFileName, outputFileName, gimpDllLocation);

            if (!rasterizeSuccess)
            {
                Int32 err = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
                throw new Win32Exception(err);
            }
        }

    /// <summary>
    /// http://msdn.microsoft.com/en-us/magazine/cc164123.aspx
    /// http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_24547525.html
    /// </summary>
    internal sealed class ExternRaster
    {

        public static bool RasterizeSvg(string inputFileName, string outputFileName, string gimpDllLocation)
        {
            bool callSuccessful = SetDllDirectory(gimpDllLocation);
            if (!callSuccessful)
            {
                throw new Exception("Could not set DLL directory");
            }
            g_type_init();
            IntPtr error1;
            IntPtr result = rsvg_pixbuf_from_file_at_size(@"E:\green.svg", -1, -1, out error1);
            if (error1 != IntPtr.Zero)
            {
                throw new Exception(Marshal.ReadInt32(error1).ToString());
            }

            IntPtr error2;
            callSuccessful = gdk_pixbuf_save(result, @"E:\green.jpg", "jpg", out error2, __arglist(null));
            if (!callSuccessful)
            {
                throw new Exception(error2.ToInt32().ToString());
            }

            return true;
        }

    }



    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Runtime.InteropServices;
使用系统组件模型;
光栅化器
{
/// 
///采用SVG图像文件类型并转换为PNG图像文件类型。
///必须安装Gimp。Gimp dll位置必须在web.config中
///文件密钥“ThreatAnalyzer DllDirectoryForGimp”。
/// 
/// http://msdn.microsoft.com/en-us/magazine/cc164123.aspx
/// http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_24547525.html
///     /// 
公共静态类光栅化器
{
//C:\Windows\System32
[DllImport(“kernel32.dll”,SetLastError=true)]
静态外部bool SetDllDirectory(字符串路径名);
[DllImport(“libgobject-2.0-0.dll”,SetLastError=true)]
静态外部无效g_type_init();
[DllImport(“librsvg-2-2.dll”,SetLastError=true)]
静态外部IntPtr rsvg_pixbuf_来自大小为的文件(字符串文件名、int宽度、int高度、out IntPtr错误);
[DllImport(“libgdk_pixbuf-2.0-0.dll”,CallingConvention=CallingConvention.Cdecl,CharSet=CharSet.Auto)]
静态外部bool gdk_pixbuf_save(IntPtr pixbuf、字符串文件名、字符串类型、out IntPtr error、参数列表);
//静态外部bool gdk_pixbuf_save_to_buffer(
/// 
///从web.config appsettings key/value获取Gimp安装DLL的位置。
///在this.webConfigFileAppSettingsImp中可以找到密钥名称
/// 
/// 
私有静态字符串GetGIMPDLLocationFromConfigurationFile(bool webapp,字符串非默认配置文件名)
{
//连接到配置文件
Configuration rasterizerConfiguration=新TA.Rasterizer.Configuration(webapp,非默认配置文件名);
//抓取配置设置
返回光栅化配置.Get(“ThreatAnalyzer DllDirectoryForGimp”);
}
/// 
/// http://msdn.microsoft.com/en-us/magazine/cc164123.aspx
///您必须在调用后检查文件是否存在,才能检查是否成功。
/// 
///要转换的SVG文件
///作为最终产品的PNG文件
/// 
公共静态void SvgToPng(字符串输入文件名、字符串输出文件名、bool webapp、字符串非默认配置文件名)
{
字符串GIMPDLLOCATION=GetGIMPDLLOCATIONFROMConfigurationFile(webapp,非默认配置文件名);
bool rasterizeSuccess=ExternRaster.RasterizeSvg(inputFileName、outputFileName、gimpdllocation);
如果(!光栅化成功)
{
Int32 err=System.Runtime.InteropServices.Marshal.GetLastWin32Error();
抛出新的Win32Exception(错误);
}
}
/// 
/// http://msdn.microsoft.com/en-us/magazine/cc164123.aspx
/// http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_24547525.html
/// 
内部密封类外部控制器
{
公共静态布尔光栅化SVG(字符串输入文件名、字符串输出文件名、字符串GIMPDLLOCATION)
{
bool callSuccessful=SetDllDirectory(gimpdllocation);
如果(!callSuccessful)
{
抛出新异常(“无法设置DLL目录”);
}
g_type_init();
IntPtr错误1;
IntPtr result=rsvg_pixbuf_来自大小为(@“E:\green.svg”,-1,-1,out error1)的文件;
如果(错误1!=IntPtr.Zero)
{
抛出新异常(Marshal.ReadInt32(error1.ToString());
}
IntPtr错误2;
callSuccessful=gdk_pixbuf_save(结果@“E:\green.jpg”,“jpg”,out error2,_arglist(null));
如果(!callSuccessful)
{
抛出新异常(error2.ToInt32().ToString());
}
返回true;
}
}
}
}
我正在从MSTest单元测试访问代码:

    /// <summary>
    ///A test for RasterizeSvg
    ///</summary>
    [TestMethod()]
    public void RasterizeSvgTest()
    {
        // arrange
        string svgFileNameInAppDirectory = "1kijvsrewxxr3mdvlwitjvyu-pie.svg";

        string currentClassLibDirectory = Environment.CurrentDirectory.Replace(@"\bin\debug\","");
        string pathToAppData = Setup.GetDataPath(); 
        string inputFileName = Path.Combine(pathToAppData, svgFileNameInAppDirectory);
        string outputFileName = Path.Combine(pathToAppData, svgFileNameInAppDirectory.Replace("svg","png"));
        bool webapp = false; 
        string nonDefaultConfigFileName = string.Empty; 

        // act
        Rasterizer.SvgToPng(inputFileName, outputFileName, webapp, nonDefaultConfigFileName);

        // assert
        bool fileExists = File.Exists(outputFileName);
        Assert.IsTrue(fileExists);

        // cleanup
        if (fileExists)
        {
            File.Delete(outputFileName);
        }
    }
//
///光栅化SVG的测试
///
[TestMethod()]
public void RasterizeSvgTest()
{
//安排
字符串svgFileNameInAppDirectory=“1kijvsrewxxr3mdvlwitjvyu pie.svg”;
字符串currentClassLibDirectory=Environment.CurrentDirectory.Replace(@“\bin\debug\”,“”);
字符串pathToAppData=Setup.GetDataPath();
string inputFileName=Path.Combine(路径到应用数据,svgFileNameInAppDirectory);
string outputFileName=Path.Combine(pathToAppData,svgFileNameInAppDirectory.Replace(“svg”,“png”));
boolwebapp=false;
string非默认配置文件名=string.Empty;
//表演
SvgToPng(输入文件名、输出文件名、webapp、非默认配置文件名);
//断言
bool fileExists=File.Exists(outputFileName);
Assert.IsTrue(fileExists);
//清理
如果(文件存在)
{
删除(outputFileName);
}
}
上面说

如果发生错误,则设置错误并返回NULL

对我来说,这可能意味着,如果成功,您的
error1
变量未设置为有效指针。您应该测试返回值
result

[DllImport("libgobject-2.0-0.dll", SetLastError = true)]
static extern void g_object_unref(IntPtr obj);
g_object_unref(result);