参数无效 - 元文件中的位图

时间:2014-02-18 13:18:58

标签: c# winforms

我遇到的问题是“每隔一次”调用此函数就会产生“参数无效”错误:

private Bitmap BitmapFromMetafile(Metafile mf, int dpi, Graphics g)
{
    int BitmapWidth = (int)(dpi * mf.Width / mf.HorizontalResolution);
    int BitmapHeight = (int)(dpi * mf.Height / mf.VerticalResolution);
    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
    g.SmoothingMode = SmoothingMode.AntiAlias;
    g.CompositingQuality = CompositingQuality.HighQuality;
    g.PixelOffsetMode = PixelOffsetMode.HighQuality;

    Bitmap bmp = new Bitmap(BitmapWidth, BitmapHeight, g);  // This is the error line "parameter is not valid"
    g = Graphics.FromImage(bmp);
    g.Clear(Color.Transparent);
    g.DrawImage(mf, 0, 0, BitmapWidth, BitmapHeight);
    return bmp;
}

我编写了导致某种类型内存问题的内容吗?

为了提供更多信息,这就是它被称为的地方:

private void GenerateMaps()
{
    string tmpNameEMF = TempFileNameAndDelete() + ".emf";
    string tmpNameEMFKeyMap = TempFileNameAndDelete() + "_keymap.emf";
    Metafile mfTargetMap = null;
    Metafile mfKeyMap = null;
    Bitmap bm = new Bitmap(1, 1);
    bm.SetResolution(TargetImgResolutionDPI, TargetImgResolutionDPI);
    try
    {
        if (TargetActiveMap != null)
        {
            using (Graphics gt = Graphics.FromImage(bm))
            {
                gt.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                gt.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                gt.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                mfTargetMap = TargetActiveMap.GetMapAsMetafile(tmpNameEMF);
                MaterializedImage_Target = BitmapFromMetafile(mfTargetMap, TargetImgResolutionDPI, gt); // This is the call
                if (OnTargetImgComplete != null)
                    OnTargetImgComplete();
            }
        }
        if (KeyMapActiveMap != null)
        {
            using (Graphics gk = Graphics.FromImage(bm))
            {
                gk.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                gk.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                gk.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                mfKeyMap = KeyMapActiveMap.GetMapAsMetafile(tmpNameEMFKeyMap);
                MaterializedImage_KeyMap = BitmapFromMetafile(mfKeyMap, KeyMapImgResolutionDPI, gk);
                if (OnKeyMapImgComplete != null)
                    OnKeyMapImgComplete();
            }
            MaterializedImage_Thumbnail = (Bitmap)mfKeyMap.GetThumbnailImage((int)MaterializedImage_KeyMap.Width * (KeyMapThumbnailImgAsPercent / 100),
                                       (int)MaterializedImage_KeyMap.Height * (KeyMapThumbnailImgAsPercent / 100), GetThumnailImageAbort,
                                        IntPtr.Zero);
            if (OnThumbnailImgComplete != null)
                OnThumbnailImgComplete();
        }
    }
    catch (Exception ex)
    {
        System.Windows.Forms.MessageBox.Show("ERROR: CMImageToWord.GenerateMaps.1: " + ex.Message);
    }

    if (mfKeyMap != null) mfKeyMap.Dispose();
    if (mfTargetMap != null) mfTargetMap.Dispose();
    try
    {
        File.Delete(tmpNameEMF);
        File.Delete(tmpNameEMFKeyMap);
    }
    catch (Exception ex)
    {
        System.Windows.Forms.MessageBox.Show("ERROR: CMImageToWord.GenerateMaps.2: " + ex.Message);
    }
}

1 个答案:

答案 0 :(得分:0)

首先确保位图最终处理完毕,图元文件大小和dpi都不能为负值。