私有字符串DecodeImage(位图图像​​) [英] private string DecodeImage(Bitmap image)

查看:88
本文介绍了私有字符串DecodeImage(位图图像​​)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我是C的初学者。

我学习了一些项目如何创建一个可以将文本转换为图像的应用程序,反之亦然。我的问题是如何使用这种方法。 iam卡住了:(



i使用这种方法



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

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;
}





和iam尝试使用decodeimage在我的文本框中显示消息,但我发现错误时debug。



and iam tried to decodeimage to show the message in my textbox, but i found error when debug.

private void button5_Click(object sender, EventArgs e)
{
    textBox1.Text = DecodeImage(pictureBox1.Image);
}





怎么调用方法,所以我们可以解码到我的文本框1?



错误信息:



参数1:无法从'System.Drawing.Image'转换为'System.Drawing。位图'



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'

推荐答案

这很简单:位图是图像,但图像不是位图。就像Apple是Fruit,但Fruit不是Apple一样。



你的方法被设置为采用一个参数,它声明为一个位图。你正在调用它并尝试传递一个Image,因此它试图将Image升级为Bitmap并且(非常正确地)说它不能这样做,因为它不知道如何不能从'System.Drawing.Image转换'to'System.Drawing.Bitmap'你可以把一个隐藏在标有Fruit的纸袋中的通用水果变成苹果。你可以抛弃继承链,但需要明确的强制转换。



有两种方法可以解决这个问题:

1)将您的方法更改为接受Image而不是Bitmap作为参数 - 然后您可以非常愉快地传递它。

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.
private string DecodeImage(Image image)





2)更改您的调用代码以提供Bitmap而不是Image,如有必要,可以通过显式转换它:



2) Change your calling code to provide a Bitmap instead of an Image, if necessary by explicitly casting it:

textBox1.Text = DecodeImage((Bitmap) pictureBox1.Image); 



在这种情况下,您需要第二个选项,因为Bitmap具有GetPixel方法,而Image不具有。


In this case, you want the second option, since Bitmap has the GetPixel method, and Image doesn't.


这篇关于私有字符串DecodeImage(位图图像​​)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆