Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello i am beginner in C#
iam learning some project how to creat an application that can convert text to image and vice versa. my problem is how to use this method. iam stuck :(

i use this method

C#
private string DecodeImage(Bitmap image)
{
    string Result = null;
    if (image.Height > 0)
    {
        if (image.Height == image.Width)
        {
            for (int y = 0; y < image.Height; y++)
            {
                for (int x = 0; x < image.Width; x++)
                {
                    int bite = (image.GetPixel(x, y).R + image.GetPixel(x, y).G + image.GetPixel(x, y).B);
                    if (bite == 0) break;
                    else Result += (char)bite;
                }
            }
        }
    }
    return Result;
}


and iam tried to decodeimage to show the message in my textbox, but i found error when debug.
C#
private void button5_Click(object sender, EventArgs e)
{
    textBox1.Text = DecodeImage(pictureBox1.Image);
}


how tocall the method , so we can decodeimage to my textbox 1?

error message :

Argument 1: cannot convert from 'System.Drawing.Image' to 'System.Drawing.Bitmap'
Posted
Updated 15-Aug-13 4:03am
v2
Comments
Sergey Alexandrovich Kryukov 15-Aug-13 9:24am    
Look at the type diagram! And don't use PictureBox (explain why, however), don't use GetPixel (prohibitively slow).
—SA
Gun Gun Febrianza 15-Aug-13 9:30am    
sir can you help me i will upload my project

1 solution

It's pretty simple: A Bitmap is an Image, but an Image isn't a Bitmap. In the same way that an Apple is a Fruit, but a Fruit isn't an Apple.

Your method is set to take a single parameter, which it declared as a Bitmap. You are calling it and trying to pass an Image, so it is trying to promote the Image to a Bitmap and (quite correctly) says it cannot do that, because it "does not know how to cannot convert from 'System.Drawing.Image' to 'System.Drawing.Bitmap'" any more than you can take a generic Fruit hidden in a paper bag labeled "Fruit" and make it into an Apple. You can cast down the inheritance chain, but it takes an explicit cast to go up.

There are two ways you could cure this:
1) Change your method to accept an Image instead of a Bitmap as it's parameter - you can then pass it either perfectly happily.
C#
private string DecodeImage(Image image)


2) Change your calling code to provide a Bitmap instead of an Image, if necessary by explicitly casting it:
C#
textBox1.Text = DecodeImage((Bitmap) pictureBox1.Image); 

In this case, you want the second option, since Bitmap has the GetPixel method, and Image doesn't.
 
Share this answer
 
Comments
Gun Gun Febrianza 15-Aug-13 10:06am    
love you sir (y)
you help me this code works like magic <3
OriginalGriff 15-Aug-13 11:00am    
You're welcome!
Thomas Daniels 15-Aug-13 10:52am    
+5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900