how to insert/embed a gif-file in a rtf-file?

  • Thread starter Thread starter D
  • Start date Start date
D

D

Hi there,

We have a program that generates a rtf-file (some 1.500 pages...) and
all of them should be signed. Problem: I'm not able to link/embed
(using insert | field) the signature-file (let's say: sign.gif), the
program works with its own templates (rtf), and I've been searching
and trying some things but after inserting, the filesize increases up
to 1,5 mb, and that's something that piece of software obviously
doesn't like. (the original 'template'/rtf is about 39 kb)

Is there a way to include/embed/... that boring sign.gif into the
rtf-file?

All tips are welcome, as long as I don't have to copy-paste for a
thousand times!

K.reg...
D
 
try following C# code to embed the image in an rtf doc, tell me if it works.

/// <summary>
/// Allows the x and y coordinates of metafile to be adjusted independently
/// </summary>
private const int MM_ANISOTROPIC = 8;


/// <summary>
/// Use the EmfToWmfBits function in the GDI+ specification to convert a
/// Enhanced Metafile to a Windows Metafile
/// </summary>
/// <param name="_hEmf">
/// A handle to the Enhanced Metafile to be converted
/// </param>
/// <param name="_bufferSize">
/// The size of the buffer used to store the Windows Metafile bits returned
/// </param>
/// <param name="_buffer">
/// An array of bytes used to hold the Windows Metafile bits returned
/// </param>
/// <param name="_mappingMode">
/// The mapping mode of the image. This control uses MM_ANISOTROPIC.
/// </param>
/// <param name="_flags">
/// Flags used to specify the format of the Windows Metafile returned
/// </param>
[DllImportAttribute("gdiplus.dll")]
private static extern uint GdipEmfToWmfBits (IntPtr _hEmf, uint _bufferSize,
byte[] _buffer, int _mappingMode, EmfToWmfBitsFlags _flags);


/// <summary>
/// Wraps the image in an Enhanced Metafile by drawing the image onto the
/// graphics context, then converts the Enhanced Metafile to a Windows
/// Metafile, and finally appends the bits of the Windows Metafile in HEX
/// to a string and returns the string.
/// </summary>
/// <param name="_image"></param>
/// <returns>
/// A string containing the bits of a Windows Metafile in HEX
/// </returns>
private string GetRtfImage(Image _image)
{

StringBuilder _rtf = null;

// Used to store the enhanced metafile
MemoryStream _stream = null;

// Used to create the metafile and draw the image
Graphics _graphics = null;

// The enhanced metafile
Metafile _metaFile = null;

// Handle to the device context used to create the metafile
IntPtr _hdc;

try
{
_rtf = new StringBuilder();
_stream = new MemoryStream();

// Get a graphics context from the RichTextBox
using(_graphics = this.CreateGraphics())
{

// Get the device context from the graphics context
_hdc = _graphics.GetHdc();

// Create a new Enhanced Metafile from the device context
_metaFile = new Metafile(_stream, _hdc);

// Release the device context
_graphics.ReleaseHdc(_hdc);
}

// Get a graphics context from the Enhanced Metafile
using(_graphics = Graphics.FromImage(_metaFile))
{

// Draw the image on the Enhanced Metafile
_graphics.DrawImage(_image, new Rectangle(0, 0, _image.Width, _image.Height));

}

// Get the handle of the Enhanced Metafile
IntPtr _hEmf = _metaFile.GetHenhmetafile();

// A call to EmfToWmfBits with a null buffer return the size of the
// buffer need to store the WMF bits. Use this to get the buffer
// size.
uint _bufferSize = GdipEmfToWmfBits(_hEmf, 0, null, MM_ANISOTROPIC,
EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault);

// Create an array to hold the bits
byte[] _buffer = new byte[_bufferSize];

// A call to EmfToWmfBits with a valid buffer copies the bits into the
// buffer an returns the number of bits in the WMF.
uint _convertedSize = GdipEmfToWmfBits(_hEmf, _bufferSize, _buffer, MM_ANISOTROPIC, EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault);


// Append the bits to the RTF string
for(int i = 0; i < _buffer.Length; ++i)
{
_rtf.Append(String.Format("{0:X2}", _buffer));
}

return _rtf.ToString();
}

finally
{
if(_graphics != null)
_graphics.Dispose();
if(_metaFile != null)
_metaFile.Dispose();
if(_stream != null)
_stream.Close();
}
}


I am also fighting for creating RTF file (Project report kindof thing) using C#, if you have any sharable things pls do forward on (e-mail address removed)
 
Back
Top