C# word open xml c图像质量下降
我正在生成图像,并使用word open xml将它们放入模板word文档中。不过,C增加了额外的“功能” 假设我们有一个彩色方块。C将使用1像素宽的光边框“升级”图片。我能避免这种行为吗 用图像替换标记图片的代码: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 =====================================
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();
}