1

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# ?

  • did you set the `InitialLONGFetchSize` to -1 for the command? https://stackoverflow.com/a/2655950/4228458 – CodingYoshi Jul 14 '17 at 19:27
  • I'm getting 'System.Data.OracleClient.OracleCommand' does not contain a definition for 'InitialLONGFetchSize' when I Try to use InitialLONGFetchSize. – Fábio Carvalho Jul 14 '17 at 19:45
  • Have you tried [creating a function](https://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:13213885403654) that takes a LONG RAW and returns a BLOB? Maybe you could alter the SQL statement to return the function's value instead of the LONG RAW value. You could also execute a procedure that takes the inputs, executes the query, and return the outputs. – Jeff Holt Jul 14 '17 at 20:01
  • @jeff6times7 yes, it's returning: "ORA-00932: Inconsistent datatypes: expected - got LONG BINARY" – Fábio Carvalho Jul 14 '17 at 20:03
  • @FábioCarvalho When you said "yes", you mean you used a function in an existing query? I recommend you go with a stored procedure, passing inputs that let it do all the work, returning data in its out parameters. The [Helsinki Declaration](http://thehelsinkideclaration.blogspot.com/) makes a very strong case for writing all database applications such that the database work is done, well, in the database. – Jeff Holt Jul 14 '17 at 21:27
  • @jeff6times7 Exactly! I just tried to convert using TO_LONG in existing query. Ok, I dont know how to do that procedure but I will see with my work colleagues if someone know how to do. There is no way to fix that in c# controller ? By the way, I Appreciate your help, thanks a lot I'll see the links you sent me. – Fábio Carvalho Jul 16 '17 at 23:31
  • @FábioCarvalho Check [this](https://docs.oracle.com/cd/E11882_01/win.112/e23174/featData.htm#ODPNT287) out. – Jeff Holt Jul 17 '17 at 02:18

0 Answers0