, 그것은 이상적인 방법하지 않을 수 있지만 제대로 작동 : 여기
var gDoc = DocX.Load(@"MauPhieuNhapCBCC.docx");
for (int f = 0; f < gDoc.Images.Count; f++) //Loop through the images of the document
{
Novacode.Image imo = gDoc.Images[f]; //Get image of the docuemnt
if (imo.Id == "imageIdInDocument") //Check the id
{
using (SD.Image newImg = SD.Image.FromFile("newImagePath")) //Load the new image for replacement
{
SD.Bitmap bMa = new SD.Bitmap(imo.GetStream(FileMode.Open, FileAccess.ReadWrite));
SD.Graphics graf = SD.Graphics.FromImage(bMa);
graf.Clear(SD.Color.White); //Set the image of the document all white
//Below we insert our new image over the one in the document
//We might need to change the image size to fit the area of our existing image
//If that were the case we give a function to resize images
SD.Image newResizeImg = meths.ResizeImage(newImg, newWidth, newHeight);
//Below we set the point(0, 0) to overlap the new image from the top left corner
graf.DrawImage(newResizeImg, new SD.Rectangle(new SD.Point(0, 0), newResizeImg.Size),
new SD.Rectangle(new SD.Point(), newResizeImg.Size), SD.GraphicsUnit.Pixel);
bMa.Save(imo.GetStream(FileMode.Create, FileAccess.Write), SD.Imaging.ImageFormat.Jpeg);
}
}
}
이미지 크기를 조정하는 기능을 :에 있었다 우리가 기본적으로 한
public SD.Image ResizeImage(SD.Image img, int newWidth, int newHeight)
{
if (img.Width < newWidth && img.Height < newHeight) return img;
using (img)
{
SD.Bitmap cpy = new SD.Bitmap(newWidth, newHeight, SD.Imaging.PixelFormat.Format32bppArgb);
using (SD.Graphics gr = SD.Graphics.FromImage(cpy))
{
gr.Clear(SD.Color.Transparent);
gr.InterpolationMode = SD.Drawing2D.InterpolationMode.HighQualityBicubic;
gr.DrawImage(img, new SD.Rectangle(0, 0, newWidth, newHeight), new SD.Rectangle(0, 0, img.Width, img.Height), SD.GraphicsUnit.Pixel);
}
return cpy;
}
}
문서의 이미지를 가져 와서 흰색으로 칠하고 같은 크기 이하의 기존 이미지 안에 새 이미지를 설정합니다. 희망이 있으면 도움이됩니다.