Hi
This is a bit long winded; but should help. I have left out preambling code.
The first method in the modWordClass was borrowed from somewhere that now escapes me, I am not the original author of it.
MarkC
/*
* resize both photos
*/
modWordClass.resizeJpgImage(childImageIn, childImageOut, 459, 610);
modWordClass.resizeJpgImage(familyImageIn, familyImageOut, 459, 610);
/*
* create a working document from our template document
*/
File.Copy(inDocx, outDocx, true);
/*
* open the working document for editing
*/
modWordDoc.modWordClass.replacePageOneImages(outDocx, childImageOut, familyImageOut, backgroundImageOut);
-------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using DocumentFormat.OpenXml.Packaging;
using System.IO;
using System.Xml;
using System.Linq.Expressions;
namespace modWordDoc
{
public class modWordClass
{
public static System.Drawing.Image FixedSize(System.Drawing.Image imgPhoto, int Width, int Height)
{
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = ((float)Width / (float)sourceWidth);
nPercentH = ((float)Height / (float)sourceHeight);
if (nPercentH < nPercentW)
{
nPercent = nPercentH;
destX = System.Convert.ToInt16((Width -
(sourceWidth * nPercent)) / 2);
}
else
{
nPercent = nPercentW;
destY = System.Convert.ToInt16((Height -
(sourceHeight * nPercent)) / 2);
}
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap bmPhoto = new Bitmap(Width, Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
imgPhoto.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.Clear(System.Drawing.Color.Red);
grPhoto.InterpolationMode =
InterpolationMode.HighQualityBicubic;
grPhoto.DrawImage(imgPhoto,
new System.Drawing.Rectangle(destX, destY, destWidth, destHeight),
new System.Drawing.Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel);
grPhoto.Dispose();
return bmPhoto;
}
public static void replaceAndFlushImage(string ImageOut, ImagePart replaceImage)
{
FileStream loFS;
Stream imageStream = replaceImage.GetStream(FileMode.Open, FileAccess.ReadWrite);
using (imageStream)
{
loFS = File.OpenRead(ImageOut);
using (loFS)
{
long lnFilesize = loFS.Length;
byte[] lbBytes = new byte[lnFilesize];
loFS.Read(lbBytes, 0, (int)lnFilesize);
imageStream.Write(lbBytes, 0, (int)lnFilesize);
imageStream.Flush();
}
}
}
public static void resizeJpgImage(string ImageIn, string ImageOut, int width, int height)
{
System.Drawing.Image imgPhoto;
System.Drawing.Image imageIn = System.Drawing.Image.FromFile(ImageIn);
using (imageIn)
{
imgPhoto = modWordDoc.modWordClass.FixedSize(imageIn, 459, 610);
using (imgPhoto)
{
imgPhoto.Save(ImageOut, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
public static void replacePageOneImages(string outDocx, string childImageOut, string familyImageOut, string backgroundImageOut)
{
WordprocessingDocument doc = WordprocessingDocument.Open(outDocx, true);
using (doc)
{
/*
* replace the background image of the document
*/
var theHeads = doc.MainDocumentPart.HeaderParts;
foreach (HeaderPart headerPart in theHeads)
{
foreach (ImagePart headImage in headerPart.ImageParts)
{
string imageUri = headImage.Uri.ToString();
if (imageUri == @"/word/media/image3.jpeg")
{
replaceAndFlushImage(backgroundImageOut, headImage);
}
}
}
/*
* replace the child and family images in the document
*/
foreach (ImagePart oneImage in doc.MainDocumentPart.ImageParts)
{
string ridPath = oneImage.Uri.ToString();
switch (ridPath)
{
case @"/word/media/image1.jpeg":
replaceAndFlushImage(childImageOut, oneImage);
break;
case @"/word/media/image2.jpeg":
replaceAndFlushImage(familyImageOut, oneImage);
break;
}
}
}
}
}
}