views:

231

answers:

2

Hi,

this is my first question on here. I'm trying to build a dial control as a custom user control in VB.NET. I'm using VS2008.

so far I have managed to rotate image using graphics.rotatetransform . however, this rotate everything. Now I have a Bitmap for the dial which should stay stable and another Bitmap for the needle which I need to rotate.

so far i've tried this:

Dim gL As Graphics = Graphics.FromImage(bmpLongNeedle)
    gL.TranslateTransform(bmpLongNeedle.Width / 2, bmpLongNeedle.Height * 0.74)
    gL.RotateTransform(angleLongNeedle)
    gL.TranslateTransform(-bmpLongNeedle.Width / 2, -bmpLongNeedle.Height * 0.74)
    gL.DrawImage(bmpLongNeedle, 0, 0)

As I understand it, the image of the needle should be rotated at angle "angleLongNeedle" although i'm placing the rotated image at 0,0. However, the result is that the Needle doesn't get drawn on the control.

any pointers as to where I might be going wrong or something else I should be doing?

Thanks in advance

A: 

First of all, why do you allocate the Graphics object from a bitmap that you then proceed to draw onto the graphics? That doesn’t make sense.

Dim gL As Graphics = Graphics.FromImage(bmpLongNeedle)
' … '
gL.DrawImage(bmpLongNeedle, 0, 0)

What you probably want is a graphics context for the whole image. You then apply the transformations to it and finally draw the bmpLongNeedle image.

Secondly, your translations look inversed: in the first step, you need to move the image to the origin (0, 0); then you rotate it, and then move it back. So the transformation should look like this:

gL.TranslateTransform(-bmpLongNeedle.Width * 0.5, -bmpLongNeedle.Height * 0.5)
gL.RotateTransform(angleLongNeedle)
gL.TranslateTransform(bmpLongNeedle.Width * 0.5, bmpLongNeedle.Height * 0.5)

Notice the inversed order of the TranslateTransforms. Also, why did you translate by 0.74 times the height, instead of half?

Konrad Rudolph
A: 
AtharvaI