1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| const int HMM_PER_INCH = 2540;
const int TWIPS_PER_INCH = 1440;
public RtfBuilder AddImage(Image image, int width, int height)
{
if (image != null)
{
String prefix = ImagePrefix(image, width, height);
//String data = ImageToHex(image);
String footer = @"}";
Byte[] bytes = ImageToByte(image);
_container.Append(prefix);
for (int i = 0; i != bytes.Length; ++i)
{
_container.AppendFormat("{0:x2}", bytes[i]);
}
//_container.Append(data);
_container.Append(footer);
}
}
private String ImagePrefix(Image image, int width, int height)
{
float xDpi, yDpi;
using (Graphics graphs = Graphics.FromImage(image))
{
xDpi = graphs.DpiX;
yDpi = graphs.DpiY;
}
int picw = (int)Math.Round((image.Width / xDpi) * HMM_PER_INCH);
int pich = (int)Math.Round((image.Height / yDpi) * HMM_PER_INCH);
int picwgoal = (int)Math.Round((width / xDpi) * TWIPS_PER_INCH);
int pichgoal = (int)Math.Round((height / yDpi) * TWIPS_PER_INCH);
return @"{\pict\wmetafile8\picw" + picw.ToString() + @"\pich" + pich.ToString()
+ @"\picwgoal" + picwgoal.ToString() + @"\pichgoal" + pichgoal.ToString() + Environment.NewLine;
}
//private String ImageToHex(Image image)
private Byte[] ImageToByte(Image image)
{
MemoryStream ms = new MemoryStream();
image.Save(ms, image.RawFormat);
return ms.ToArray();
//String hex = BitConverter.ToString(bytes,0);
//return hex.Replace("-", String.Empty);
} |
Partager