CentralServerDemo.ExRichTextBox.GetImagePrefix C# (CSharp) Method

GetImagePrefix() private method

Creates the RTF control string that describes the image being inserted. This description (in this case) specifies that the image is an MM_ANISOTROPIC metafile, meaning that both X and Y axes can be scaled independently. The control string also gives the images current dimensions, and its target dimensions, so if you want to control the size of the image being inserted, this would be the place to do it. The prefix should have the form ... {\pict\wmetafile8\picw[A]\pich[B]\picwgoal[C]\pichgoal[D] where ... A = current width of the metafile in hundredths of millimeters (0.01mm) = Image Width in Inches * Number of (0.01mm) per inch = (Image Width in Pixels / Graphics Context's Horizontal Resolution) * 2540 = (Image Width in Pixels / Graphics.DpiX) * 2540 B = current height of the metafile in hundredths of millimeters (0.01mm) = Image Height in Inches * Number of (0.01mm) per inch = (Image Height in Pixels / Graphics Context's Vertical Resolution) * 2540 = (Image Height in Pixels / Graphics.DpiX) * 2540 C = target width of the metafile in twips = Image Width in Inches * Number of twips per inch = (Image Width in Pixels / Graphics Context's Horizontal Resolution) * 1440 = (Image Width in Pixels / Graphics.DpiX) * 1440 D = target height of the metafile in twips = Image Height in Inches * Number of twips per inch = (Image Height in Pixels / Graphics Context's Horizontal Resolution) * 1440 = (Image Height in Pixels / Graphics.DpiX) * 1440
The Graphics Context's resolution is simply the current resolution at which windows is being displayed. Normally it's 96 dpi, but instead of assuming I just added the code. According to Ken Howe at pbdr.com, "Twips are screen-independent units used to ensure that the placement and proportion of screen elements in your screen application are the same on all display systems." Units Used ---------- 1 Twip = 1/20 Point 1 Point = 1/72 Inch 1 Twip = 1/1440 Inch 1 Inch = 2.54 cm 1 Inch = 25.4 mm 1 Inch = 2540 (0.01)mm
private GetImagePrefix ( Image _image ) : string
_image Image
return string
        private string GetImagePrefix(Image _image) {

            StringBuilder _rtf = new StringBuilder();

            // Calculate the current width of the image in (0.01)mm
            int picw = (int)Math.Round((_image.Width / xDpi) * HMM_PER_INCH);

            // Calculate the current height of the image in (0.01)mm
            int pich = (int)Math.Round((_image.Height / yDpi) * HMM_PER_INCH);

            // Calculate the target width of the image in twips
            int picwgoal = (int)Math.Round((_image.Width / xDpi) * TWIPS_PER_INCH);

            // Calculate the target height of the image in twips
            int pichgoal = (int)Math.Round((_image.Height / yDpi) * TWIPS_PER_INCH);

            // Append values to RTF string
            _rtf.Append(@"{\pict\wmetafile8");
            _rtf.Append(@"\picw");
            _rtf.Append(picw);
            _rtf.Append(@"\pich");
            _rtf.Append(pich);
            _rtf.Append(@"\picwgoal");
            _rtf.Append(picwgoal);
            _rtf.Append(@"\pichgoal");
            _rtf.Append(pichgoal);
            _rtf.Append(" ");

            return _rtf.ToString();
        }