3 Attachment(s)
[RESOLVED] RotateTransform gradient not in sync
So doing some drawing in a drop down and when I dont transform it it draws as expected. However when I transform the brush 90 degrees, for ex. it seems to also transform the rectangle. However if I adjust for that then it draws outside the border as if the rectangle isnt transformed. I think my rectangle and brush rectangle are out of alignment causing it to tile or draw out of the border.
Its an 11 x 10 pixel rectangle and the first column of pixels is the "margin" resulting in a 10 x 10 square.
No transform
Attachment 162953
Transform 90
Attachment 162955
Transform 45
Attachment 162957
Code:
Pen pen = new Pen(Color.Black, 1);
Graphics rectGraphics;
Bitmap img = new Bitmap(11, 10);
rectGraphics = Graphics.FromImage(img);
rectGraphics.DrawLine(pen, 2, 0, 9, 0); //rounded rectangle
rectGraphics.DrawLine(pen, 10, 1, 10, 8); //rounded rectangle
rectGraphics.DrawLine(pen, 9, 9, 2, 9); //rounded rectangle
rectGraphics.DrawLine(pen, 1, 8, 1, 1); //rounded rectangle
Rectangle innards = new Rectangle(2, 1, 8, 8);
LinearGradientBrush lgb = new LinearGradientBrush(innards, Color.White, Color.White, 0, false);
ColorBlend cb = new ColorBlend();
cb.Positions = new[] { 0.0f, 0.5f, 1.0f };
cb.Colors = new[] { Color.White, Color.Pink, Color.Red };
lgb.InterpolationColors = cb;
lgb.RotateTransform(90);
rectGraphics.FillRectangle(lgb, innards); //fill rectangle innards
pen.Dispose();
rectGraphics.Dispose();
1 Attachment(s)
Re: RotateTransform gradient not in sync
Never fails, soon as I post a thread I figure it out.
So seems an overload of the LinearGradienBrush has an angle argument. So I changed to that overload and commented the RotateTransform and it works as needed now. :thumb:
Changes:
Code:
LinearGradientBrush lgb = new LinearGradientBrush(innards, Color.Black, Color.Black, 45);
...
...
//lgb.RotateTransform(90);
Attachment 162961