我正在尝试将Oracle Long Raw类型转换为c#中的byte []。当我得到byte []时,我需要创建一个图像并保存在我的内容的某个文件夹中,之后我需要返回我的视图的路径,并从visjs库中为节点提供在画布中显示我的图像的路径。
问题是,当我尝试使用memorystream生成我的图像时,我得到“参数无效”错误。我尝试了一些替代方案,但都失败了。
首先:我尝试了这个解决方案:https://stackoverflow.com/a/3801289/5692322但是我遇到了同样的错误。
然后,我尝试了这个:https://stackoverflow.com/a/16576471/5692322我得到了相同的错误。
所以..我尝试在我的javascript上渲染我的图像,但仍然无效。当我尝试使用View时,我得到了一张空白图片。
这是我目前的代码:
{
PropertyInfo propertyToEvaluate = property;
for (int i = 0; i <= dr.FieldCount - 1; i++)
{
if (nomePropriedade.Equals(dr.GetName(i).ToLower()))
{
object valor = null;
if (!object.ReferenceEquals(valor, DBNull.Value))
{
if (!String.IsNullOrEmpty(dr.GetValue(i).ToString()))
{
if (object.ReferenceEquals(propertyToEvaluate.PropertyType, typeof(int)) ||
object.ReferenceEquals(propertyToEvaluate.PropertyType, typeof(int?)))
{
valor = Convert.ToInt32(dr.GetValue(i));
}
else if (object.ReferenceEquals(propertyToEvaluate.PropertyType, typeof(short)) ||
object.ReferenceEquals(propertyToEvaluate.PropertyType, typeof(short?)))
{
valor = short.Parse(dr.GetValue(i).ToString());
}
else if (object.ReferenceEquals(propertyToEvaluate.PropertyType, typeof(long)) ||
object.ReferenceEquals(propertyToEvaluate.PropertyType, typeof(long?)))
{
valor = Convert.ToInt64(dr.GetValue(i));
}
else if (object.ReferenceEquals(propertyToEvaluate.PropertyType, typeof(decimal)) ||
object.ReferenceEquals(propertyToEvaluate.PropertyType, typeof(decimal?)))
{
valor = Convert.ToDecimal(dr.GetValue(i));
}
else if (object.ReferenceEquals(propertyToEvaluate.PropertyType, typeof(double)) ||
object.ReferenceEquals(propertyToEvaluate.PropertyType, typeof(double?)))
{
valor = Convert.ToDouble(dr.GetValue(i));
}
else if (object.ReferenceEquals(propertyToEvaluate.PropertyType, typeof(float)) ||
object.ReferenceEquals(propertyToEvaluate.PropertyType, typeof(float?)))
{
valor = float.Parse(dr.GetValue(i).ToString());
}
else if (object.ReferenceEquals(propertyToEvaluate.PropertyType, typeof(byte)) ||
object.ReferenceEquals(propertyToEvaluate.PropertyType, typeof(byte?)))
{
valor = byte.Parse(dr.GetValue(i).ToString());
}
else if (object.ReferenceEquals(propertyToEvaluate.PropertyType, typeof(byte[])) ||
object.ReferenceEquals(propertyToEvaluate.PropertyType, typeof(byte[])))
{
//Get Long Raw and conver to byte[]
valor = Encoding.ASCII.GetBytes(dr.GetValue(i).ToString());
}
else if (object.ReferenceEquals(propertyToEvaluate.PropertyType, typeof(bool)))
{
valor = (dr.GetValue(i).Equals("0")) ? false : true; //Convert.ToBoolean(dr.GetValue(i));
}
else if (object.ReferenceEquals(propertyToEvaluate.PropertyType, typeof(DateTime)) || object.ReferenceEquals(propertyToEvaluate.PropertyType, typeof(DateTime?)))
{
valor = Convert.ToDateTime(dr.GetValue(i));
}
else if (propertyToEvaluate.PropertyType.IsEnum)
{
Type enumType = propertyToEvaluate.PropertyType;
valor = EnumExtensao.Converter(enumType, dr.GetValue(i).ToString().Trim());
}
else
{
valor = dr.GetValue(i).ToString().Trim();
}
if ((!object.ReferenceEquals(dr.GetValue(i), DBNull.Value)))
{
propertyToEvaluate.SetValue(item, valor, null);
}
break;
}
}
}
}
}
Controller.cs
//Convert byte[] to base64
string imagemStr = Convert.ToBase64String(obterPremissas[i].nom_ident_denho);
//Save image in path and return to View
//This approach it's returning all images with same size and blank
var path = SaveImage(imagemStr, "Foo");
public static string SaveImage(string ImgStr, string imgName)
{
String path = System.Web.HttpContext.Current.Server.MapPath("~/Content/Imagens/ImageStorage");
//Check if directory exist
if (!System.IO.Directory.Exists(path))
{
System.IO.Directory.CreateDirectory(path); //Create directory if it doesn't exist
}
string imageName = imgName + ".jpg";
//set the image path
string imgPath = Path.Combine(path, imageName);
byte[] imageBytes = Convert.FromBase64String(ImgStr);
Image x = (Bitmap)((new ImageConverter()).ConvertFrom(imageBytes));
System.IO.File.WriteAllBytes(imgPath, imageBytes);
path = path + "\\" + imageName;
return path;
}
正如评论所说,我在这种方法上得到了空白图像,由于这张空白图像,现在我正在尝试这个:
Bitmap newBitmap = GetImageFromByteArray(obterPremissas[i].nom_ident_denho);
public static Bitmap GetImageFromByteArray(byte[] byteArray)
{
Bitmap bm = (Bitmap)_imageConverter.ConvertFrom(byteArray);
if (bm != null && (bm.HorizontalResolution != (int)bm.HorizontalResolution ||
bm.VerticalResolution != (int)bm.VerticalResolution))
{
bm.SetResolution((int)(bm.HorizontalResolution + 0.5f),
(int)(bm.VerticalResolution + 0.5f));
}
return bm;
}
但在第一行Bitmap bm = (Bitmap)_imageConverter.ConvertFrom(byteArray);
当我使用memoryStream“参数无效”时,我收到同样的错误。 任何人都知道如何在C#中处理Oracle Long Raw?