Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. DrawString with opacity option not print properly

DrawString with opacity option not print properly

Scheduled Pinned Locked Moved C#
help
20 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Le rner

    Hi all, i draw a string diagonal on my Panel, and draw this text with opacity option get color for text like below:

    PrintPageEventArgs pe;

    int X = 0;
    int Y = 0;

    RectangleF rect2 = panel2.ClientRectangle;

    float image_draw_wd = panel2.Width;

    float image_draw_ht = panel2.Height;

    Brush myBrush = new SolidBrush(Color.FromArgb(128, Color.Blue));

    StringFormat stringFormat = new StringFormat();
    stringFormat.Alignment = StringAlignment.Center;
    stringFormat.FormatFlags = StringFormatFlags.LineLimit;
    stringFormat.Trimming = StringTrimming.Word;

    SizeF sz = pe.MeasureString(panel2.watermarkText, panel2.watermarkFont, (int)image_draw_wd, stringFormat);

    double angle = 0;
    double angleRadians = Math.Atan2(image_draw_ht, image_draw_wd);
    angle = angleRadians * 180.0 / Math.PI;

    pe.TranslateTransform((rect2.Location.X + rect2.Width / 2), (rect2.Location.Y + rect2.Height / 2));
    pe.RotateTransform((float)angle + flip180Image);
    pe.TranslateTransform(-(rect2.Location.X + rect2.Width / 2), -(rect2.Location.Y + rect2.Height / 2));

    X = (int)(panel2.Width - sz.Width) / 2;
    Y = (int)(panel2.Height - sz.Height) / 2;

    RectangleF dr_rect = new RectangleF(new Point(X, Y, new Size(image_draw_wd , ((int)sz.Height) + extra_margin));

    pe.DrawString(panel2.watermarkText, panel2.watermarkFont, myBrush, dr_rect, stringFormat);

    but when i print it, the text not fully visible if i draw it horizontally its working fine. i cant understand where is the problem please help me for this. thanks alot

    OriginalGriffO Offline
    OriginalGriffO Offline
    OriginalGriff
    wrote on last edited by
    #4

    That should crash: pe is null

    PrintPageEventArgs pe;
    ...
    SizeF sz = pe.MeasureString(panel2.watermarkText, panel2.watermarkFont, (int)image_draw_wd, stringFormat);

    You need to use the PrintPageEventArgs that is passed in as part of the PrintPage event or the metrics you use for MeasureString won't work anyway as they aren't relevant to the device context you are drawing on - even if you construct one somewhere and don't show it here.

    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

    L 1 Reply Last reply
    0
    • OriginalGriffO OriginalGriff

      That should crash: pe is null

      PrintPageEventArgs pe;
      ...
      SizeF sz = pe.MeasureString(panel2.watermarkText, panel2.watermarkFont, (int)image_draw_wd, stringFormat);

      You need to use the PrintPageEventArgs that is passed in as part of the PrintPage event or the metrics you use for MeasureString won't work anyway as they aren't relevant to the device context you are drawing on - even if you construct one somewhere and don't show it here.

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

      L Offline
      L Offline
      Le rner
      wrote on last edited by
      #5

      its not null its come from here

      private void PrintDocument1_PrintPage(object sender, PrintPageEventArgs pe)

      OriginalGriffO 2 Replies Last reply
      0
      • L Le rner

        its not null its come from here

        private void PrintDocument1_PrintPage(object sender, PrintPageEventArgs pe)

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #6

        Thanks - I assumed it would, but when it's explicitly shown as a new declaration it needs to be confirmed.

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        L 1 Reply Last reply
        0
        • L Le rner

          its not null its come from here

          private void PrintDocument1_PrintPage(object sender, PrintPageEventArgs pe)

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #7

          When you say "the text not fully visible" what exactly do you mean?

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          L 1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            Thanks - I assumed it would, but when it's explicitly shown as a new declaration it needs to be confirmed.

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

            L Offline
            L Offline
            Le rner
            wrote on last edited by
            #8

            sorry to poor presentation but please understand and help me

            1 Reply Last reply
            0
            • OriginalGriffO OriginalGriff

              When you say "the text not fully visible" what exactly do you mean?

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

              L Offline
              L Offline
              Le rner
              wrote on last edited by
              #9

              is there any option to show the image how can i upload my drawing image to describe u better

              OriginalGriffO 1 Reply Last reply
              0
              • L Le rner

                is there any option to show the image how can i upload my drawing image to describe u better

                OriginalGriffO Offline
                OriginalGriffO Offline
                OriginalGriff
                wrote on last edited by
                #10

                No, because we get some idiots here who would post ... ummm ... objectional material X| You wouldn't believe what tries to get through moderation ATM, and we can't moderate every post, there aren't enough of us volunteers to do that. And you don't have sufficient rep yet to store images here. You could put it up on Dropbox / Google and provide a link?

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                L 1 Reply Last reply
                0
                • OriginalGriffO OriginalGriff

                  No, because we get some idiots here who would post ... ummm ... objectional material X| You wouldn't believe what tries to get through moderation ATM, and we can't moderate every post, there aren't enough of us volunteers to do that. And you don't have sufficient rep yet to store images here. You could put it up on Dropbox / Google and provide a link?

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

                  L Offline
                  L Offline
                  Le rner
                  wrote on last edited by
                  #11

                  actual design like this Actual_Label.png - Google Drive[^] and printed like this Label_print.png - Google Drive[^] but when i draw string horizontally its print like original one more thing when i check its print preview its looks perfect,so why its not print ok thats why i cant understand where is the problem

                  OriginalGriffO 2 Replies Last reply
                  0
                  • L Le rner

                    actual design like this Actual_Label.png - Google Drive[^] and printed like this Label_print.png - Google Drive[^] but when i draw string horizontally its print like original one more thing when i check its print preview its looks perfect,so why its not print ok thats why i cant understand where is the problem

                    OriginalGriffO Offline
                    OriginalGriffO Offline
                    OriginalGriff
                    wrote on last edited by
                    #12

                    OK - I'm trying to duplicate exactly what you have, but ... your code won't compile without errors and I don't want to fix them in case I "fix them wrong". For example:

                    RectangleF dr_rect = new RectangleF(new Point(X, Y, new Size(image_draw_wd, ((int)sz.Height) + extra_margin));

                    Missing a close bracket (at a guess, after the "Point(X, Y" is where it goes). PrintPageEventArgs doesn't contain methods MeasureString, TranslateTransform, and RotateTransform: PrintPageEventAgrs.Graphics does. If that is copy'n'pasted from your app, then it's possible you are testing an older version of your source - in which case the display fault may no longer exist if you fix that code.

                    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

                    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                    1 Reply Last reply
                    0
                    • L Le rner

                      actual design like this Actual_Label.png - Google Drive[^] and printed like this Label_print.png - Google Drive[^] but when i draw string horizontally its print like original one more thing when i check its print preview its looks perfect,so why its not print ok thats why i cant understand where is the problem

                      OriginalGriffO Offline
                      OriginalGriffO Offline
                      OriginalGriff
                      wrote on last edited by
                      #13

                      If I bodge up your code (fix the errors, use substitutes for external bits I don't have) it works ok in preview for me:

                         private void FrmMain\_Shown(object sender, EventArgs e)
                              {
                              PrintDocument pd = new PrintDocument();
                              pd.PrintPage += Pd\_PrintPage;
                              PrintPreviewDialog pdi = new PrintPreviewDialog { Document = pd };
                              pdi.ShowDialog();
                              }
                          private void Pd\_PrintPage(object sender, PrintPageEventArgs pe)
                              {
                              int X = 0;
                              int Y = 0;
                      
                              RectangleF rect2 = panel2.ClientRectangle;
                      
                              float image\_draw\_wd = panel2.Width;
                      
                              float image\_draw\_ht = panel2.Height;
                      
                              Brush myBrush = new SolidBrush(Color.FromArgb(128, Color.Blue));
                      
                              StringFormat stringFormat = new StringFormat();
                              stringFormat.Alignment = StringAlignment.Center;
                              stringFormat.FormatFlags = StringFormatFlags.LineLimit;
                              stringFormat.Trimming = StringTrimming.Word;
                      
                              SizeF sz = pe.Graphics.MeasureString("A line of text to display", Font, (int)image\_draw\_wd, stringFormat);
                      
                      
                              double angle = 0;
                              double angleRadians = Math.Atan2(image\_draw\_ht, image\_draw\_wd);
                              angle = angleRadians \* 180.0 / Math.PI;
                      
                              pe.Graphics.TranslateTransform((rect2.Location.X + rect2.Width / 2), (rect2.Location.Y + rect2.Height / 2));
                              pe.Graphics.RotateTransform((float)angle);// + flip180Image);
                              pe.Graphics.TranslateTransform(-(rect2.Location.X + rect2.Width / 2), -(rect2.Location.Y + rect2.Height / 2));
                      
                      
                              X = 10;// (int)(panel2.Width - sz.Width) / 2;
                              Y = 10; // (int)(panel2.Height - sz.Height) / 2;
                      
                              RectangleF dr\_rect = new RectangleF(new Point(X, Y), new Size((int) image\_draw\_wd, ((int)sz.Height) + 10));
                      
                              pe.Graphics.DrawString("A line of text to display", Font, myBrush, dr\_rect, stringFormat);
                              }
                      

                      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

                      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                      L 1 Reply Last reply
                      0
                      • OriginalGriffO OriginalGriff

                        If I bodge up your code (fix the errors, use substitutes for external bits I don't have) it works ok in preview for me:

                           private void FrmMain\_Shown(object sender, EventArgs e)
                                {
                                PrintDocument pd = new PrintDocument();
                                pd.PrintPage += Pd\_PrintPage;
                                PrintPreviewDialog pdi = new PrintPreviewDialog { Document = pd };
                                pdi.ShowDialog();
                                }
                            private void Pd\_PrintPage(object sender, PrintPageEventArgs pe)
                                {
                                int X = 0;
                                int Y = 0;
                        
                                RectangleF rect2 = panel2.ClientRectangle;
                        
                                float image\_draw\_wd = panel2.Width;
                        
                                float image\_draw\_ht = panel2.Height;
                        
                                Brush myBrush = new SolidBrush(Color.FromArgb(128, Color.Blue));
                        
                                StringFormat stringFormat = new StringFormat();
                                stringFormat.Alignment = StringAlignment.Center;
                                stringFormat.FormatFlags = StringFormatFlags.LineLimit;
                                stringFormat.Trimming = StringTrimming.Word;
                        
                                SizeF sz = pe.Graphics.MeasureString("A line of text to display", Font, (int)image\_draw\_wd, stringFormat);
                        
                        
                                double angle = 0;
                                double angleRadians = Math.Atan2(image\_draw\_ht, image\_draw\_wd);
                                angle = angleRadians \* 180.0 / Math.PI;
                        
                                pe.Graphics.TranslateTransform((rect2.Location.X + rect2.Width / 2), (rect2.Location.Y + rect2.Height / 2));
                                pe.Graphics.RotateTransform((float)angle);// + flip180Image);
                                pe.Graphics.TranslateTransform(-(rect2.Location.X + rect2.Width / 2), -(rect2.Location.Y + rect2.Height / 2));
                        
                        
                                X = 10;// (int)(panel2.Width - sz.Width) / 2;
                                Y = 10; // (int)(panel2.Height - sz.Height) / 2;
                        
                                RectangleF dr\_rect = new RectangleF(new Point(X, Y), new Size((int) image\_draw\_wd, ((int)sz.Height) + 10));
                        
                                pe.Graphics.DrawString("A line of text to display", Font, myBrush, dr\_rect, stringFormat);
                                }
                        

                        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

                        L Offline
                        L Offline
                        Le rner
                        wrote on last edited by
                        #14

                        i already mention in preview its ok

                        OriginalGriffO 2 Replies Last reply
                        0
                        • L Le rner

                          i already mention in preview its ok

                          OriginalGriffO Offline
                          OriginalGriffO Offline
                          OriginalGriff
                          wrote on last edited by
                          #15

                          Yes, but your code doesn't compile, so that has to be taken with a pinch of salt! :laugh:

                          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

                          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                          1 Reply Last reply
                          0
                          • L Le rner

                            i already mention in preview its ok

                            OriginalGriffO Offline
                            OriginalGriffO Offline
                            OriginalGriff
                            wrote on last edited by
                            #16

                            And if I change my code:

                                private void FrmMain\_Shown(object sender, EventArgs e)
                                    {
                                    PrintDocument pd = new PrintDocument();
                                    pd.PrintPage += Pd\_PrintPage;
                                    PrintDialog pdi = new PrintDialog { Document = pd };
                                    pdi.ShowDialog();
                                    pd.Print();
                            

                            It prints fine to PDF, XPS, OneNote, ... So the code seems OK as far as I can see. So ... what happens if you fix the compiler errors, and print to - say - OneNote?

                            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

                            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                            L 1 Reply Last reply
                            0
                            • OriginalGriffO OriginalGriff

                              And if I change my code:

                                  private void FrmMain\_Shown(object sender, EventArgs e)
                                      {
                                      PrintDocument pd = new PrintDocument();
                                      pd.PrintPage += Pd\_PrintPage;
                                      PrintDialog pdi = new PrintDialog { Document = pd };
                                      pdi.ShowDialog();
                                      pd.Print();
                              

                              It prints fine to PDF, XPS, OneNote, ... So the code seems OK as far as I can see. So ... what happens if you fix the compiler errors, and print to - say - OneNote?

                              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

                              L Offline
                              L Offline
                              Le rner
                              wrote on last edited by
                              #17

                              private void Pd_PrintPage(object sender, PrintPageEventArgs pe)
                              {

                                      string watermarkText = "Your Name © 2015, All Rights Reserved";
                                      Font watermarkFont = new Font("Microsoft Sans Serif", 18, FontStyle.Bold);
                              
                                      int X = 0;
                                      int Y = 0;
                              
                                      RectangleF rect2 = panel2.ClientRectangle;
                              
                                      float image\_draw\_wd = panel2.Width;
                              
                                      float image\_draw\_ht = panel2.Height;
                              
                                      Brush myBrush = new SolidBrush(Color.FromArgb(128, Color.Blue));
                              
                                      StringFormat stringFormat = new StringFormat();
                                      stringFormat.Alignment = StringAlignment.Center;
                                      stringFormat.FormatFlags = StringFormatFlags.LineLimit;
                                      stringFormat.Trimming = StringTrimming.Word;
                              
                              
                                      SizeF sz = pe.Graphics.MeasureString(watermarkText, watermarkFont, (int)image\_draw\_wd, stringFormat);
                              
                              
                                      double angle = 0;
                                      double angleRadians = Math.Atan2(image\_draw\_ht, image\_draw\_wd);
                                      angle = angleRadians \* 180.0 / Math.PI;
                              
                                      pe.Graphics.TranslateTransform((rect2.Location.X + rect2.Width / 2), (rect2.Location.Y + rect2.Height / 2));
                                      pe.Graphics.RotateTransform((float)angle);// + flip180Image);
                                      pe.Graphics.TranslateTransform(-(rect2.Location.X + rect2.Width / 2), -(rect2.Location.Y + rect2.Height / 2));
                              
                              
                                      X = 10;
                                      X = (int)(panel2.Width - sz.Width) / 2;
                                      Y = 10;
                                      Y = (int)(panel2.Height - sz.Height) / 2;
                              
                                      RectangleF dr\_rect = new RectangleF(new Point(X, Y), new Size((int)image\_draw\_wd, ((int)sz.Height) + 10));
                              
                                      pe.Graphics.DrawString(watermarkText, watermarkFont, myBrush, dr\_rect, stringFormat);
                                  }
                              

                              preview , pdf n all looks same n fine why its not print on actual printer my model is HP deskjet gt 5811

                              OriginalGriffO 1 Reply Last reply
                              0
                              • L Le rner

                                private void Pd_PrintPage(object sender, PrintPageEventArgs pe)
                                {

                                        string watermarkText = "Your Name © 2015, All Rights Reserved";
                                        Font watermarkFont = new Font("Microsoft Sans Serif", 18, FontStyle.Bold);
                                
                                        int X = 0;
                                        int Y = 0;
                                
                                        RectangleF rect2 = panel2.ClientRectangle;
                                
                                        float image\_draw\_wd = panel2.Width;
                                
                                        float image\_draw\_ht = panel2.Height;
                                
                                        Brush myBrush = new SolidBrush(Color.FromArgb(128, Color.Blue));
                                
                                        StringFormat stringFormat = new StringFormat();
                                        stringFormat.Alignment = StringAlignment.Center;
                                        stringFormat.FormatFlags = StringFormatFlags.LineLimit;
                                        stringFormat.Trimming = StringTrimming.Word;
                                
                                
                                        SizeF sz = pe.Graphics.MeasureString(watermarkText, watermarkFont, (int)image\_draw\_wd, stringFormat);
                                
                                
                                        double angle = 0;
                                        double angleRadians = Math.Atan2(image\_draw\_ht, image\_draw\_wd);
                                        angle = angleRadians \* 180.0 / Math.PI;
                                
                                        pe.Graphics.TranslateTransform((rect2.Location.X + rect2.Width / 2), (rect2.Location.Y + rect2.Height / 2));
                                        pe.Graphics.RotateTransform((float)angle);// + flip180Image);
                                        pe.Graphics.TranslateTransform(-(rect2.Location.X + rect2.Width / 2), -(rect2.Location.Y + rect2.Height / 2));
                                
                                
                                        X = 10;
                                        X = (int)(panel2.Width - sz.Width) / 2;
                                        Y = 10;
                                        Y = (int)(panel2.Height - sz.Height) / 2;
                                
                                        RectangleF dr\_rect = new RectangleF(new Point(X, Y), new Size((int)image\_draw\_wd, ((int)sz.Height) + 10));
                                
                                        pe.Graphics.DrawString(watermarkText, watermarkFont, myBrush, dr\_rect, stringFormat);
                                    }
                                

                                preview , pdf n all looks same n fine why its not print on actual printer my model is HP deskjet gt 5811

                                OriginalGriffO Offline
                                OriginalGriffO Offline
                                OriginalGriff
                                wrote on last edited by
                                #18

                                At a guess, driver problems. I can't test that - I have a Samsung colour laser as I got fed up with inkjets never working when I needed them ...

                                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

                                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                                "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                                L 1 Reply Last reply
                                0
                                • OriginalGriffO OriginalGriff

                                  At a guess, driver problems. I can't test that - I have a Samsung colour laser as I got fed up with inkjets never working when I needed them ...

                                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

                                  L Offline
                                  L Offline
                                  Le rner
                                  wrote on last edited by
                                  #19

                                  ok thank u so much for your support

                                  OriginalGriffO 1 Reply Last reply
                                  0
                                  • L Le rner

                                    ok thank u so much for your support

                                    OriginalGriffO Offline
                                    OriginalGriffO Offline
                                    OriginalGriff
                                    wrote on last edited by
                                    #20

                                    No problem - sorry I can't do more!

                                    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

                                    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                                    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                                    1 Reply Last reply
                                    0
                                    Reply
                                    • Reply as topic
                                    Log in to reply
                                    • Oldest to Newest
                                    • Newest to Oldest
                                    • Most Votes


                                    • Login

                                    • Don't have an account? Register

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • World
                                    • Users
                                    • Groups