通过使用C#字符串变量的base64字符串。错误:指​​数数组的边界之外 [英] Passing base64 string using c# string variable. Error: Index was outside the bounds of the array

查看:85
本文介绍了通过使用C#字符串变量的base64字符串。错误:指​​数数组的边界之外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

还是一个新手。
我想,以取代长期的Base64字符串变量,但一直得到错误:指数数组的边界之外

场景:我从检索SQL数据库例如完整的base64字符串数据:图像/ GIF; BASE64,R0lGODlhAQABAIAAAAAAAP /// yH5BAEAAAAALAAAAAABAAEAAAIBRAA7

我再拆,我想这是后一节中的字符串,

 公共SqlDataReader的阅读器;
    公共字符串ClientSigImg;
    公共字符串ClientSigImg1;    ClientSigImg =读者[0]的ToString();
    ClientSigImg1 = ClientSigImg.Split(,)[1];

所以上面的base64字符串,如下面用户输入的变化和代替手工输入

 串的base64 = @R0lGODlhAQABAIAAAAAAAP /// yH5BAEAAAAALAAAAAABAAEAAAIBRAA7
    字节[] = imageBytes Convert.FromBase64String的(Base64);

我想与替换长字符串:

 串的base64 = @+ ClientSigImg1 +;
    字节[] = imageBytes Convert.FromBase64String的(Base64);


解决方案

看起来好像:

  ClientSigImg =读卡器[0]的ToString();

不会在这一个逗号返回一个字符串(请调试和确认)。这意味着,你会看到错误你所描述的,当这条线code运行的:

  ClientSigImg1 = ClientSigImg.Split('')[1];

这是因为指数1将不存在。所以,请检查你实际上得到您的查询返回正确的数据。

此外,一旦您的查询返回不正确的数据,您可以整理你的code和简单的使用:

 字节[] = imageBytes Convert.FromBase64String(ClientSigImg1);

这是因为:

 串的base64 = @+ ClientSigImg1 +;

实际上并没有做比以前和你的字符串后面添加空字符串以外的任何其他,因此,不添加任何东西!

Still A Newbie. I would like to replace the long Base64 string with a variable but keep getting error: Index was outside the bounds of the array.

Scenario: I am retrieving the full base64 string from SQL database e.g data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7

I then split the string for the section that I want that is after the ','.

    public SqlDataReader reader;
    public String ClientSigImg;
    public String ClientSigImg1;

    ClientSigImg = reader[0].ToString();
    ClientSigImg1 = ClientSigImg.Split(',')[1];

So the above base64 string changes on user input and instead of manual input like below

    string base64 = @"R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"
    byte[] imageBytes = Convert.FromBase64String(base64);

I would like to replace that long string with:

    string base64 = @""+ ClientSigImg1 +"";
    byte[] imageBytes = Convert.FromBase64String(base64);

解决方案

It looks as though:

ClientSigImg = reader[0].ToString();

Is NOT returning a string with a comma in it (please debug and confirm). This means that you will see the error you are describing when this line of code runs:

ClientSigImg1 = ClientSigImg.Split(',')[1];

This is because index 1 will not exist. So, check that you are actually getting the correct data returned from your query.

In addition, once your query does return the correct data you can tidy your code and simply use:

byte[] imageBytes = Convert.FromBase64String(ClientSigImg1);

That is because:

string base64 = @""+ ClientSigImg1 +"";

Doesn't actually do anything other than add empty string before and after your string, hence, doesn't add anything!

这篇关于通过使用C#字符串变量的base64字符串。错误:指​​数数组的边界之外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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