从图像中读取文本后,为什么消息框为空?

时间:2014-10-19 17:01:52

标签: c# .net image c#-4.0 image-processing

我正在将图像转换为文本但它返回空白消息框

private void button1_Click(object sender, EventArgs e)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            Image image = Image.FromFile(@"C:\Users\home\Documents\visual studio 2013\Projects\WindowsFormsApplication2\WindowsFormsApplication2\jack.jpg");

            // Convert Image to byte[]
            System.Drawing.Imaging.ImageFormat format = ImageFormat.Jpeg;
            image.Save(ms, format);
            byte[] imageBytes = ms.ToArray();

            // Convert byte[] to Base64 String
            string base64String = Convert.ToBase64String(imageBytes);
            MessageBox.Show(base64String.ToString());
        }

我在调试器中检查过,分配给base64变量的值是:

/9j/4AAQSkZJRgABAQEASABIAAD/4Q/qRXhpZgAATU0AKgAAAAgAAwEaAAUAAAABAAAAMgEbAAUAAAABAAAAOgEoAAMAAAABAAIAAAAAAEIAAABIAAAAAQAAAEgAAAABAAYBAwADAAAAAQAGAAABGgAFAAAAAQAAAJABGwAFAAAAAQAAAJgBKAADAAAAAQACAAACAQAEAAAAAQAAAKACAgAEAAAAAQAAD0IAAAAAAAAASAAAAAEAAABIAAAAAf/Y/+AAEEpGSUYAAQEBAEgASAAA/9sAQwAGBAUGBQQGBgUGBwcGCAoQCgoJCQoUDg8MEBcUGBgXFBYWGh0lHxobIxwWFiAsICMmJykqKRkfLTAtKDAlKCko/9sAQwEHBwcKCAoTCgoTKBoWGigoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgo.......

图像:

enter image description here

2 个答案:

答案 0 :(得分:1)

您正试图在弹出框中显示180,000个字符。

超过一定长度,它将失败并显示一个空白框。

例如,这应该显示:

MessageBox.Show(base64String.Substring(0, 1000));

如果您需要完整的东西,请使用TextBox或输出到文件。

答案 1 :(得分:0)

谢谢你帮了很多的人,现在我有了这个代码,就像魅力一样

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MODI;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Upload(object sender, EventArgs e)
    {
        string filePath = Server.MapPath("~/Uploads/" + Path.GetFileName(FileUpload1.PostedFile.FileName));
        FileUpload1.SaveAs(filePath);
        string extractText = this.ExtractTextFromImage(filePath);
        lblText.Text = extractText.Replace(Environment.NewLine, "<br />");
    }

    private string ExtractTextFromImage(string filePath)
    {
        Document modiDocument = new Document();
        modiDocument.Create(filePath);
        modiDocument.OCR(MiLANGUAGES.miLANG_ENGLISH);
        MODI.Image modiImage = (modiDocument.Images[0] as MODI.Image);
        string extractedText = modiImage.Layout.Text;
        modiDocument.Close();
        return extractedText;
    }

}