locked
Convert various graphic formats into a Bitmap format RRS feed

  • Question

  • I need to convert image formats (*jpg,*tiff...) into a Bitmap format in order to manipulate the images with SetPixel method of the Bitmap class. But if I load a Bitmap(file) the image obtained, however is good at glance , gives wrong results with GetPixel() and SetPixel().

    Thanks in advanced 

    Thursday, April 3, 2014 12:08 PM

Answers

  • Hello molokko,

    Please clarify how you convert those formats to bitmap format, after reading the functions you mentioned above, it seems jpg file is also a supported scenario which means you may not need to convert it:

    See something below:

    Bitmap.SetPixel Method

    private void SetPixel_Example(PaintEventArgs e)
    {
    
        // Create a Bitmap object from a file.
        Bitmap myBitmap = new Bitmap("Grapes.jpg");
    
        // Draw myBitmap to the screen.
        e.Graphics.DrawImage(myBitmap, 0, 0, myBitmap.Width,
            myBitmap.Height);
    
        // Set each pixel in myBitmap to black. 
        for (int Xcount = 0; Xcount < myBitmap.Width; Xcount++)
        {
            for (int Ycount = 0; Ycount < myBitmap.Height; Ycount++)
            {
                myBitmap.SetPixel(Xcount, Ycount, Color.Black);
            }
        }
    
        // Draw myBitmap to the screen again.
        e.Graphics.DrawImage(myBitmap, myBitmap.Width, 0,
            myBitmap.Width, myBitmap.Height);
    }
    

    Bitmap.GetPixel Method

    private void GetPixel_Example(PaintEventArgs e)
    {
    
        // Create a Bitmap object from an image file.
        Bitmap myBitmap = new Bitmap("Grapes.jpg");
    
        // Get the color of a pixel within myBitmap.
        Color pixelColor = myBitmap.GetPixel(50, 50);
    
        // Fill a rectangle with pixelColor.
        SolidBrush pixelBrush = new SolidBrush(pixelColor);
        e.Graphics.FillRectangle(pixelBrush, 0, 0, 100, 100);
    }
    

    What's more, if it is a supported image in System.Drawing, just check the following:

    http://msdn.microsoft.com/en-us/library/twss4wb0(v=vs.90).aspx

    class Program
    {
        static void Main(string[] args)
        {
            // Load the image.
            System.Drawing.Image image1 = System.Drawing.Image.FromFile(@"C:\test.bmp");
    
            // Save the image in JPEG format.
            image1.Save(@"C:\test.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
    
            // Save the image in GIF format.
            image1.Save(@"C:\test.gif", System.Drawing.Imaging.ImageFormat.Gif);
    
            // Save the image in PNG format.
            image1.Save(@"C:\test.png", System.Drawing.Imaging.ImageFormat.Png);        
        }
    }
    

    Regards,



    Barry Wang
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Barry Wang Tuesday, April 15, 2014 8:16 AM
    Friday, April 4, 2014 7:38 AM