I'm trying to convert Oracle Long Raw type to byte[] in c#. When I get the byte[] I need create a image and save in some folder on my content, after this I need return the path to my view and give the nodes from visjs library the path to show my image in canvas.
The problem is, when I try use memorystream to generate my image I'm getting the "Parameter is not valid" error. I tryed some alternatives, but they all failed.
First: I Tryed this solution: https://stackoverflow.com/a/3801289/5692322 but I'm getting the same error.
Then, I tryed this: https://stackoverflow.com/a/16576471/5692322 and I got the same erros.
So.. I tryed render my image on my javascript, but still doesn't work. I got a blank image when I try on my View.
This is my current code:
{
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;
}
As comment say, I'm getting blank image on this approach, and because of this blank image, now I'm trying this:
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;
}
But in the first line, Bitmap bm = (Bitmap)_imageConverter.ConvertFrom(byteArray);
I'm getting the same error when I used memoryStream "Parameter is not valid". Anyone knows how deal with Oracle Long Raw in C# ?