question

JonJacobs-9073 avatar image
0 Votes"
JonJacobs-9073 asked JonJacobs-9073 commented

Where should I post this C# gdi+ question?

In C# I have this code:
private void DrawPageSeparator(int i, int Offset, Bitmap b)
{
if (i < 1) return;
Color Sep = Color.Black;
Pen pen = new Pen(Sep, 2);

         g.DrawLine(pen, 0, Offset, b.Width-1, Offset);
     }

Instead of Black the image gets a light gray line. If I use Color.Red, I still get the same light gray line.
Why?

dotnet-csharp
· 7
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

try making the pen a little wider

0 Votes 0 ·

Thank you, that did the trick. The pen has to be at least 3 pixels wide

0 Votes 0 ·

@JonJacobs-9073
I used the code similar to yours but did not encounter your problem. Could you please provide a more complete code for us to test?
What is your version of Visual Studio and .Net framework?

         private void button1_Click(object sender, EventArgs e)
         {
             Graphics graphics = this.CreateGraphics();
             Color Sep = Color.Red;
             Pen pen = new Pen(Sep, 2);
             graphics.DrawLine(pen, 0,5, 100, 50);
         }

80390-4.png


0 Votes 0 ·
4.png (1.5 KiB)

When the Bitmat is an existing one loaded from file, the code works fine. But when the bitmap is created in memory: DocImage = new Bitmap(923, 1200, PixelFormat.Format32bppArgb);
g = Graphics.FromImage(DocImage);
g.Clear(Color.White);

Then the pen has to be at least 3 pixels wide. To me that is weird, but there it is...


0 Votes 0 ·

I still can't reproduce it, but anyway, the problem is solved.
@vb2ae, could you please turn your comment into an answer so that JonJacobs can accept it?
Members who have the same problem in the future will be able to find a solution more quickly, I am not sure if you have this permission, if not, please let me know and let me do it.

0 Votes 0 ·

The effect seems to depend on combination of properties of Graphics object, such as CompositingQuality, InterpolationMode, SmoothingMode, PixelOffsetMode. If you choose values that correspond to low quality, you will obtain different results.

0 Votes 0 ·

That's interesting, Viorel-1. Thank you for your comment. That would mean I get different properties from creating a bitmap in memory vs reading one from disk. That is definitely worth exploring with a careful study of the different property values from both methods.

0 Votes 0 ·

0 Answers