Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,
I have stored the image taken from openfile dialog box and stored in form of byte[] to sqlserver 2012 database.But when i convert byte[] to bitmapImage so that it can be binded to wpf image control it gives error msg "No imaging component suitable to complete this operation was found."
Code i have written is :
private void ViewImage_Click(object sender, RoutedEventArgs e)
       {
           db = myHelper.GetData();
          // byte[] rawData = new byte[0];
           var q = (from xx in db.VehicleGroups where xx.id == 1011 select xx).FirstOrDefault();
              byte[] rawData = q.Imagepath;
              image1.Source = LoadImage(rawData);


       }
       private static BitmapImage LoadImage(byte[] imageData)
       {
           if (imageData == null || imageData.Length == 0) return null;
           var image = new BitmapImage();
           using (var mem = new MemoryStream(imageData))
           {
               mem.Position = 0;
               image.BeginInit();
               image.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
               image.CacheOption = BitmapCacheOption.OnLoad;
               image.UriSource = null;
               image.StreamSource = mem;
               image.EndInit();
           }
           image.Freeze();
           return image;
       }




to save the image in database code written was:
 private void btnBrowse_Click(object sender, RoutedEventArgs e)
        {
            
            OpenFileDialog ofd = new OpenFileDialog();
            Nullable<bool> result = ofd.ShowDialog();
            if (result == true)
            {
                txtBrowseFile.Text = ofd.FileName;
            }
        }

in global area:
 BitmapImage bitmapImage;
VehicleGroup objtblVehicleGroup ;

then a method:
 private void fillbitmap()
        {
            #region load image with StreamSrouce
            bitmapImage = new BitmapImage();
            bitmapImage.BeginInit();
            bitmapImage.StreamSource = System.IO.File.OpenRead(txtBrowseFile.Text);
            bitmapImage.EndInit();
            //now, the Position of the StreamSource is not in the begin of the stream.

            image1.Source = bitmapImage;

            #endregion
        }

public Byte[] BufferFromImage(BitmapImage imageSource)
       {
           Stream stream = imageSource.StreamSource;
           Byte[] buffer = null;
           if (stream != null && stream.Length > 0)
           {
               using (BinaryReader br = new BinaryReader(stream))
               {
                   buffer = br.ReadBytes((Int32)stream.Length);
               }
           }

           return buffer;
       }


C#
private void btnSave_Click(object sender, RoutedEventArgs e)
   {
         
          
               fillbitmap();
              
               objtblVehicleGroup = new VehicleGroup();

               db = myHelper.GetData();
               if (selObject.id == -1)
               {
                   objtblVehicleGroup = new VehicleGroup();
                   objtblVehicleGroup.CreatedDate = (DateTime?)System.DateTime.Now;
                   objtblVehicleGroup.IsDel = false;
               
               objtblVehicleGroup.VehicleGroupName = txtVehicleGroup.Text;
               objtblVehicleGroup.VehcileCost = Convert.ToDecimal( txtprice.Text);

               objtblVehicleGroup.Imagepath = BufferFromImage(bitmapImage);
                       db.AddToVehicleGroups(objtblVehicleGroup);

                       db.SaveChanges();
                       var userid = objtblVehicleGroup.id;
               }
               
   }
Posted
Updated 4-Sep-13 4:39am
v3
Comments
Sergey Alexandrovich Kryukov 4-Sep-13 12:55pm    
What's in rawData? The name imagePath suggests it's some path, but it should be the content of the image file.
—SA
gaurav786mishra 5-Sep-13 3:06am    
From the open file dialog box image is selected and that path is kept in txtBrowseFile.Text further from text box it is used in :
private void fillbitmap()
{
#region load image with StreamSrouce
bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = System.IO.File.OpenRead(txtBrowseFile.Text);
bitmapImage.EndInit();
//now, the Position of the StreamSource is not in the begin of the stream.

image1.Source = bitmapImage;

#endregion
}
The bitmap generated from here is then used in passed to :

public Byte[] BufferFromImage(BitmapImage imageSource)
{
Stream stream = imageSource.StreamSource;
Byte[] buffer = null;
if (stream != null && stream.Length > 0)
{
using (BinaryReader br = new BinaryReader(stream))
{
buffer = br.ReadBytes((Int32)stream.Length);
}
}

return buffer;
}

which returns a byte array and then that byte array is stored in database.
Sergey Alexandrovich Kryukov 5-Sep-13 3:18am    
You did not answer my question. If you need to add more code, do it in your question, use "Improve question".
—SA
gaurav786mishra 5-Sep-13 4:22am    
Ok Thank you for ur reply.Actually rawdata is a byte array which contains the bytes in array format coming from database.I am getting the rawdata array filled with numbers on each index of array.
I guess the problem is i am converting path to byte array and saving in database..If that i am doing..then how can i extract image from image path as wpf has no file upload control?

 
Share this answer
 
Comments
gaurav786mishra 5-Sep-13 10:30am    
Hi ,Vamshi(Krish)
The above code gives error :Buffer size is not sufficient.

public static BitmapImage ByteToImage(byte[] imageData)
{

byte[] buffer = imageData;

var width = 100; // for example
var height = 100; // for example
var dpiX = 96d;
var dpiY = 96d;
var pixelFormat = PixelFormats.Pbgra32; // for example
var bytesPerPixel = (pixelFormat.BitsPerPixel + 7) * 8;
var stride = bytesPerPixel * width;

var bitmap = BitmapSource.Create(width, height, dpiX, dpiY,
pixelFormat, null, buffer, stride);
return (BitmapImage)bitmap;
}
Dev Krish 5-Sep-13 23:53pm    
Try this for that error

http://stackoverflow.com/questions/8492563/bitmapsource-create-error-buffer-size-is-not-large-enough
C#
public static CachedBitmap ByteToImage(byte[] buffer, int width, int height, PixelFormat format)
        {
            var stride = ((width * format.BitsPerPixel + 31) / 32) * 4;
            var image = BitmapSource.Create(width, height, 96d, 96d, format, null, buffer, stride);

            return (CachedBitmap)image;
        }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900