Click here to Skip to main content
15,894,460 members
Home / Discussions / C#
   

C#

 
GeneralRe: Rotate printing Pin
damichab21-Apr-09 19:15
damichab21-Apr-09 19:15 
GeneralRe: Rotate printing Pin
damichab21-Apr-09 19:18
damichab21-Apr-09 19:18 
GeneralRe: Rotate printing Pin
Luc Pattyn22-Apr-09 0:31
sitebuilderLuc Pattyn22-Apr-09 0:31 
GeneralRe: Rotate printing Pin
damichab22-Apr-09 1:23
damichab22-Apr-09 1:23 
GeneralRe: Rotate printing Pin
Luc Pattyn22-Apr-09 2:08
sitebuilderLuc Pattyn22-Apr-09 2:08 
GeneralRe: Rotate printing Pin
damichab22-Apr-09 4:18
damichab22-Apr-09 4:18 
GeneralRe: Rotate printing Pin
Luc Pattyn22-Apr-09 5:36
sitebuilderLuc Pattyn22-Apr-09 5:36 
GeneralRe: Rotate printing Pin
damichab22-Apr-09 21:01
damichab22-Apr-09 21:01 
Hi Luc,

I am sorry to keep bothering you, but I am still having problems.
Once you pointed out to me that I had to multiply out the draw commands, I got most of the label printing correctly. All except the rich text section which is how all this started in the first place.

Rich text provides some special problems of its own. It needs to use the Graphics.GetHdc() which seems to totally ignore any Matrix formatting. So this necessitates the use of another bitmap which is set to a standard size based on twips. But try as I May, I still cannot get the output to print clearly.

The code below is adapted from various web examples. Before I tried to draw the rich text onto a bitmap, it was very clear and sharp. This method is called as one of the Draw commands from the very first example at the top of this thread.



        // private float mmToInches = 1.0f / 25.4f;
// private float xMultiplyer = mmToInches * e.Graphics.DpiX;
// private float yMultiplyer = mmToInches * e.Graphics.DpiY;
// private Rectangle rect = new Rectangle(LabelElement.Left, LabelElement.Top, LabelElement.Width, LabelElement.Height);

//Draw(e.Graphics, rect, xMultiplyer, yMultiplyer, someRichText);

public static void Draw(Graphics graphics, RectangleF mm, float xScale, float yScale, string richText)
{
    float mmToTwips = (1f / 10f) * 567f; // 567 twips per cm, 1440 twips per inch 

    RichTextBox edit = new RichTextBox();
    edit.Rtf = richText;

    SafeNativeMethods.RECT twips;
    twips.Left = 0;
    twips.Top = 0;
    twips.Right = (int)(mm.Width * mmToTwips);
    twips.Bottom = (int)(mm.Height * mmToTwips);

    using (Bitmap drawArea = new Bitmap((int)(twips.Right), (int)(twips.Bottom)))
    {
        using (Graphics drawAreaGraphics = Graphics.FromImage(drawArea))
        {
            IntPtr hdc = drawAreaGraphics.GetHdc();

            SafeNativeMethods.FORMATRANGE fmtRange;
            fmtRange.chrg.cpMax = -1;           //Indicate character from to character to 
            fmtRange.chrg.cpMin = 0;
            fmtRange.hdc = hdc;                 //Use the same DC for measuring and rendering
            fmtRange.hdcTarget = hdc;           //Point at printer hDC
            fmtRange.rc = twips;                //Indicate the area on page to print
            fmtRange.rcPage = twips;            //Indicate size of page

            IntPtr wParam = IntPtr.Zero;
            wParam = new IntPtr(1);

            //Get the pointer to the FORMATRANGE structure in memory
            IntPtr lParam = IntPtr.Zero;
            lParam = Marshal.AllocCoTaskMem(Marshal.SizeOf(fmtRange));
            Marshal.StructureToPtr(fmtRange, lParam, false);

            SafeNativeMethods.SendMessage(edit.Handle, SafeNativeMethods.EM_FORMATRANGE, wParam, lParam);

            //Free the block of memory allocated
            Marshal.FreeCoTaskMem(lParam);

            //Release the device context handle obtained by a previous call
            drawAreaGraphics.ReleaseHdc(hdc);
        }
        graphics.DrawImage(drawArea, mm.Left * xScale, mm.Top * yScale);
    }
}

GeneralRe: Rotate printing Pin
Luc Pattyn23-Apr-09 1:19
sitebuilderLuc Pattyn23-Apr-09 1:19 
GeneralRe: Rotate printing Pin
damichab23-Apr-09 2:06
damichab23-Apr-09 2:06 
GeneralRe: Rotate printing Pin
Luc Pattyn23-Apr-09 2:44
sitebuilderLuc Pattyn23-Apr-09 2:44 
GeneralRe: Rotate printing Pin
damichab23-Apr-09 3:31
damichab23-Apr-09 3:31 
GeneralRe: Rotate printing Pin
Luc Pattyn23-Apr-09 3:36
sitebuilderLuc Pattyn23-Apr-09 3:36 
GeneralRe: Rotate printing Pin
Luc Pattyn23-Apr-09 5:01
sitebuilderLuc Pattyn23-Apr-09 5:01 
GeneralRe: Rotate printing Pin
damichab22-Apr-09 21:15
damichab22-Apr-09 21:15 
QuestionWebBrowser.ScriptErrorsSuppressed = true and self signed certifcates Pin
jo H21-Apr-09 13:52
jo H21-Apr-09 13:52 
Questionanyone has any problem with Milestone 4 Cosmos? Pin
john231321-Apr-09 11:51
john231321-Apr-09 11:51 
AnswerRe: anyone has any problem with Milestone 4 Cosmos? Pin
Christian Graus21-Apr-09 14:04
protectorChristian Graus21-Apr-09 14:04 
Questionconvert time and minutes Pin
onetreeup21-Apr-09 8:58
onetreeup21-Apr-09 8:58 
AnswerRe: convert time and minutes Pin
PIEBALDconsult21-Apr-09 9:08
mvePIEBALDconsult21-Apr-09 9:08 
GeneralRe: convert time and minutes Pin
onetreeup21-Apr-09 9:21
onetreeup21-Apr-09 9:21 
AnswerRe: convert time and minutes Pin
Edwin Brunner21-Apr-09 9:23
Edwin Brunner21-Apr-09 9:23 
GeneralRe: convert time and minutes Pin
onetreeup21-Apr-09 9:36
onetreeup21-Apr-09 9:36 
GeneralRe: convert time and minutes Pin
Edwin Brunner21-Apr-09 9:50
Edwin Brunner21-Apr-09 9:50 
Questionhow to change property of form in an other started thread? Pin
Sajjad Izadi21-Apr-09 8:08
Sajjad Izadi21-Apr-09 8:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.