Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.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
C# word open xml c图像质量下降_C#_Image_Openxml Sdk - Fatal编程技术网

C# word open xml c图像质量下降

C# word open xml c图像质量下降,c#,image,openxml-sdk,C#,Image,Openxml Sdk,我正在生成图像,并使用word open xml将它们放入模板word文档中。不过,C增加了额外的“功能” 假设我们有一个彩色方块。C将使用1像素宽的光边框“升级”图片。我能避免这种行为吗 用图像替换标记图片的代码: protected static void fillImage(SdtBlock imageControl, int ID) { // Remove the placeholder =====================================

我正在生成图像,并使用word open xml将它们放入模板word文档中。不过,C增加了额外的“功能”

假设我们有一个彩色方块。C将使用1像素宽的光边框“升级”图片。我能避免这种行为吗

用图像替换标记图片的代码:

 protected static void fillImage(SdtBlock imageControl, int ID)
    {
        // Remove the placeholder =========================================
        imageControl.SdtProperties.GetFirstChild<ShowingPlaceholder>().Remove();
        // Embed image ====================================================
        var relationshipID = "someName" + ID;
        ImagePart imagePart = _document.MainDocumentPart.AddImagePart(ImagePartType.Png, relationshipID);
        var ending = ".png";
        var folder = _imageFolder;
        var imagePath = Path.Combine(folder, _name, "image" + ID + ending);

        // Resize =========================================================
        //widthInPixels / HorizontalResolutionInDPI * 914400
        int width;
        int height;
        double ratio;
        using (Image image = Image.FromFile(imagePath, true))
        {
                some code to set width and height (from image.height and image.width)
        }

        var extent = imageControl.Descendants<WP.Extent>().Single();
        extent.Cx = width;
        extent.Cy = height;
        var extents = imageControl.Descendants<DW.Extents>().Single();
        extents.Cx = width;
        extents.Cy = height;
        using (var stream = new FileStream(imagePath, FileMode.Open))
        {
            imagePart.FeedData(stream);
        }
        // Replace the relationship ID
        var blip = imageControl.Descendants<DW.Blip>().Single();
        blip.Embed = relationshipID;
    }

您能否进一步澄清您的问题?显示添加图片和保存文档的代码。您是否随时调整图像的大小?一些Windows质量较差的调整大小算法会在纯色图像周围留下模糊的边缘。我真的这样做了,我并不为此感到骄傲。如果我什么也不做,最终的图像将是“标记图像”的大小。所以我改变了标记图片的大小,使其与我生成的图片一样接近。有没有办法保持生成图像的准确大小?
       public void RemoveContentControlsAndKeepContents(Document document)
    {
        if (document == null)
        {
            throw new ArgumentNullException("document");
        }

        CustomXmlPartCore customXmlPartCore = new CustomXmlPartCore(this.NamespaceUri);
        CustomXmlPart customXmlPart = customXmlPartCore.GetCustomXmlPart(document.MainDocumentPart);
        XmlDocument customPartDoc = new XmlDocument();

        if (customXmlPart != null)
        {
            using (XmlReader reader = XmlReader.Create(customXmlPart.GetStream(FileMode.Open, FileAccess.Read)))
            {
                customPartDoc.Load(reader);
            }
        }

        RemoveContentControlsAndKeepContents(document.Body, customPartDoc.DocumentElement);
        document.Save();
    }