Search code examples
c#aforge

Aforge Image Rotation results in a red X


I am trying to rotate an image using AForge. Here is the code (in the paint event method for a picture box):

int radius = 10; 
Image img = new Bitmap("file_path.png");
Bitmap image = new Bitmap(img, radius, radius);

//arbitrary angle of 67
RotateBilinear ro = new RotateBilinear(67, true);
image = ro.Apply(image);
Graphics g = e.Graphics;
g.DrawImage(image, 100, 100);

If I take out the RotateBilinear line and the ro.Apply line it draws the original image just fine but when i try to rotate it, the background of the form turns white and a red x with an outline of red around the form appears. Anyone know the problem?


Solution

  • Try putting a try/catch around your code.

    int radius = 10; 
    Image img = new Bitmap("file_path.png");
    Bitmap image = new Bitmap(img, radius, radius);
    //arbitrary angle of 67
    
    try
    {
        image = AForge.Imaging.Image.Clone(image, PixelFormat.Format8bppIndexed);
        RotateBilinear ro = new RotateBilinear(67, true);
        image = ro.Apply(image);
    }
    catch (Exception e)
    { 
        Debug.WriteLine(string.Format("Caught Exception {0}\n{1}", e.GetType(), e.Message);
    } 
    
    Graphics g = e.Graphics;
    g.DrawImage(image, 100, 100);
    

    If my suspicions are right, you will have the red x disappear, the image will be rendered un-rotated. AND you will have some trace statements in you output window in Visual Studio telling you the Exception Type and the Exception Message. That information should give you some idea of what it is that is going wrong.