The EntitySpaces Community

Share and learn about the EntitySpaces Architecture.
Welcome to The EntitySpaces Community Sign in | Join | Help
in
Home Forums Photos

How to cast ByteArray

Last post 08-09-2008, 6:53 AM by econner. 3 replies.
Sort Posts: Previous Next
  •  08-08-2008, 6:01 PM 10633

    How to cast ByteArray

    I have a database with an field type of image. EntitySpaces defines this column as esSystemType.ByteArray (please see below).  How is the best way to cast this after retrieving it from a query?

       public esQueryItem ImageSmallBlob
      {
       get
       {
        return new esQueryItem(this, CategoryMetadata.ColumnNames.ImageSmallBlob, esSystemType.ByteArray);
       }
      }

    //Does not work
    byte[] byteImage = (byte[])categoryCollection.Query.ImageSmallBlob

    byte[] byteImage = categoryCollection.Query.ImageSmallBlob.Cast(??????)

     

     

  •  08-08-2008, 10:36 PM 10635 in reply to 10633

    Re: How to cast ByteArray

    Perhaps this will help:

    Code:
            private static byte[] ConvertImageToByteArray(System.Drawing.Image imageToConvert, System.Drawing.Imaging.ImageFormat formatOfImage) { 
                byte[] result; 
    
                try { 
                    using (System.IO.MemoryStream ms = new MemoryStream()) { 
                        imageToConvert.Save(ms,formatOfImage); 
                        result = ms.ToArray(); 
                    } 
                } catch (System.Exception) { 
                    throw;
                } 
    
                return result; 
            }
    
            //When you are ready to convert the byte array back to an image, you can include the following code in your method. 
            public void UseImageStoredAsByte(byte[] byteArray) {
    
                System.Drawing.Image newImage;
    
                using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(byteArray, 0, byteArray.Length)) {
                    memoryStream.Write(byteArray, 0, byteArray.Length);
                    newImage = System.Drawing.Image.FromStream(memoryStream, true);
                    // work with image here. 
                    // You'll need to keep the MemoryStream open for 
                    // as long as you want to work with your new image. 
                }
            }
  •  08-08-2008, 11:51 PM 10638 in reply to 10633

    Re: How to cast ByteArray

    I'm lost. Why are you trying to convert an esQueryItem into a byte array? Once you call categoryCollection.Query.Load(), each entity's ImageSmallBlob Property should be a byte array.

    byte[] byteImage = categoryCollection[0].ImageSmallBlob


    David Neal Parsons
    www.entityspaces.net
  •  08-09-2008, 6:53 AM 10644 in reply to 10638

    Re: How to cast ByteArray

    Late night of programming :-)

    After posting this I actually looped the collection and was able to get each image.

     

View as RSS news feed in XML