Saving the Image from WPF control to SQL Server DB - sql-server

I have an application written in WPF 3.5 which at some point it saves the data in including an image in SQL Server, this is part of the code in saving the data (note this.pictureImage is a WPF Image control):-
using (SqlCommand command = myConnection.CreateCommand())
{
String sqlInsertCommand = "INSERT INTO Info_Id (idNumber, FirstName, Nationality, Image) VALUES (#idNumber, #firstName, #nationality, #image)";
command.CommandText = sqlInsertCommand;
command.CommandType = System.Data.CommandType.Text;
command.Parameters.AddWithValue("#idNumber", this.cardIdTextBlock.Text);
command.Parameters.AddWithValue("#firstName", this.fullNameTextBlock.Text);
command.Parameters.AddWithValue("#nationality", this.nationaltyTextBlock.Text);
command.Parameters.AddWithValue("#image", this.pictureImage);
command.ExecuteNonQuery();
}
After I run this and click on the saving to DB button I got the following error.
No mapping exists from object type System.Windows.Controls.Image to a known managed provider
In the SQL Server database I have a row with (Picture (Image, null)).
Please advise me. And thank you.

You can't save an Image control in your database. Instead you should encode the image in the Image control's Source property by an appropriate bitmap encoder and store the encoded buffer as byte array.
byte[] buffer;
var bitmap = pictureImage.Source as BitmapSource;
var encoder = new PngBitmapEncoder(); // or one of the other encoders
encoder.Frames.Add(BitmapFrame.Create(bitmap));
using (var stream = new MemoryStream())
{
encoder.Save(stream);
buffer = stream.ToArray();
}
// now store buffer in your DB

Related

Data storing in a mobile application(windows 8)

I need to store some data to use for a windows8 based mobile application. Data needed to be reuse. For an example need to store 4 phone numbers to send messages and another one to send calls. How can I store data in here. I have heard about isolated storage. Is it the way or can I connect it to a database. Will it application be too heavy if it is connected to a database?
Not sure what you mean by connect to a database.
In Windows Phone 8, Isolated Storage refers to storage each app has on the phone, and I don't think (I am not sure actually) other apps can access it. Basically if you need to save something it would look like that.
The following code is to save something:
IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
//create new file
using (StreamWriter writeFile = new StreamWriter(new IsolatedStorageFileStream("myFile.txt", FileMode.Create, FileAccess.Write, myIsolatedStorage)))
{
string someTextData = "This is some text data to be saved in a new text file in the IsolatedStorage!";
writeFile.WriteLine(someTextData);
writeFile.Close();
}
To access that file at anytime you would just do this:
IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile("myFile.txt", FileMode.Open, FileAccess.Read);
using (StreamReader reader = new StreamReader(fileStream))
{ //Visualize the text data in a TextBlock text
this.text.Text = reader.ReadLine();
}
Here is the link. http://www.geekchamp.com/tips/all-about-wp7-isolated-storage-read-and-save-text-files
Isolated Storage would allow you to permenately store files there and retrieve it even when the user quits their application.

Silverlight saving webcam image into the server folder

I have created a silverlight project that captures the image through the webcam and stores it in my local hard, but I don't want to save it on the local hard I want to store it in my project folder then store the path in SQL database.
Please Help!
You could try and save the image directly as a bitstream to the DB maybe?
private static ImageElementContract ImageToImageElementContract(Image image)
{
WriteableBitmap bmp = new WriteableBitmap(image, null);
ImageTools.ExtendedImage myImage = new ImageTools.ExtendedImage();
myImage = ImageExtensions.ToImage(bmp);
MemoryStream mStream = new MemoryStream();
JpegEncoder encoder = new JpegEncoder();
encoder.Quality = 100;
encoder.Encode(myImage, mStream);
imageContract.ImageStream = mStream.ToArray();
return imageContract;
}
Should give you a stream, which you can pass to the DB and put back together if you need it again. It's hard to tell what your using it for. There's not much detail in your question

Convert memory stream to BitmapImage?

I have an image that was originally a PNG that I have converted to a byte[] and saved in a database. Originally, I simply read the PNG into a memory stream and converted the stream into a byte[]. Now I want to read the byte[] back and convert it to a BitmapImage, so that I can bind a WPF Image control to it.
I am seeing a lot of contradictory and confusing code online to accomplish the task of converting a byte[] to a BitmapImage. I am not sure whether I need to add any code due to the fact that the image was originally a PNG.
How does one convert a stream to a BitmapImage?
This should do it:
using (var stream = new MemoryStream(data))
{
var bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.StreamSource = stream;
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.EndInit();
bitmap.Freeze();
}
The BitmapCacheOption.OnLoad is important in this case because otherwise the BitmapImage might try to access the stream when loading on demand and the stream might already be closed.
Freezing the bitmap is optional but if you do freeze it you can share the bitmap across threads which is otherwise impossible.
You don't have to do anything special regarding the image format - the BitmapImage will deal with it.
using (var stream = new MemoryStream(data))
{
var bi = BitmapFrame.Create(stream , BitmapCreateOptions.IgnoreImageCache, BitmapCacheOption.OnLoad);
}

Load image into memory immediately

I need to open all frames from Tiff image in WPF into memory and then delete the source. And after that I eventually need to render that image (resized according to window size). My solution is quite slow and I cannot delete file source before the first require. Any best practices?
Use CacheOption = BitmapCacheOption.OnLoad
This option can be used with the BitmapImage.CacheOption property or as an argument to BitmapDecoder.Create() If you want to access multiple frames once the images is loaded you'll have to use BitmapDecoder.Create. In either case the file will be loaded fully and closed.
See also my answer to this question
Update
The following code works perfectly for loading in all the frames of an image and deleting the file:
var decoder = BitmapDecoder.Create(new Uri(imageFileName), BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
List<BitmapFrame> images = decoder.Frames.ToList();
File.Delete(imageFileName);
You can also access decoder.Frames after the file is deleted, of course.
This variant also works if you prefer to open the stream yourself:
List<BitmapFrame> images;
using(var stream = File.OpenRead(imageFileName))
{
var decoder = BitmapDecoder.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
images = decoder.Frames.ToList();
}
File.Delete(imageFileName);
In either case it is more efficient than creating a MemoryStream because a MemoryStream keeps two copies of the data in memory at once: The decoded copy and the undecoded copy.
I figured it out. I have to use MemoryStream:
MemoryStream ms = new MemoryStream(File.ReadAllBytes(image));
TiffBitmapDecoder decoder = new TiffBitmapDecoder(ms, BitmapCreateOptions.None, BitmapCacheOption.None);
List<BitmapFrame> images = new List<BitmapFrame>();
foreach (BitmapFrame frame in decoder.Frames) images.Add(frame);

WPF application, freeing resources (images)

i have a slideshow application showing some images. I have a simple usercontrol that displays an image. I read the images from an XMl file and each time i create a new instance on a new usercontrol and show it in my application. At some point i need to "refresh" theimages (e.g. new version) i remove all my usercontrols from the application and "null" them - then i try to delete all images on disc. However, this gives me an exception that the resource "myimage.png" is in use by another process and cannot be deleted. What do i have to do to "release" my resources so that i can delete them?
cheers,
I've looked at the WPF source code using .NET Refector and found this answer to a related question that might be a better solution for you. Set the BitmapImage.CacheOption to BitmapCacheOption.OnLoad forces the image data into memory and closes the file immediately.
Using that option, you can delete the from the file system at any time afterwards.
I had a similar problem - I needed to provide an image preview using a temporary file for the image. Once the preview was closed I wanted to delete the file.
I used an explicit stream instead of a URI to load the image:
BitmapImage imageSource = new BitmapImage();
FileStream imageStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read | FileShare.Delete);
imageSource.BeginInit();
imageSource.StreamSource = imageStream;
imageSource.EndInit();
// imagePreview is a WPF Image
imagePreview.Source = imageSource;
Then when it was time to close the UI and release the image I explicitly closed the stream:
BitmapImage imageSource = imagePreview.Source as BitmapImage;
imagePreview.Source = null;
if (null != imageSource)
{
System.IO.Stream stream = imageSource.StreamSource;
imageSource.StreamSource = null;
if (null != stream)
{
stream.Close();
}
// now the file can be deleted
File.Delete(filePath);
}
I used an explicit stream instead of a URI to load the image:
BitmapImage imageSource = new BitmapImage();
FileStream imageStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read | FileShare.Delete);
imageSource.BeginInit();
imageSource.StreamSource = imageStream;
imageSource.EndInit();
// imagePreview is a WPF Image
imagePreview.Source = imageSource;
Then when it was time to close the UI and release the image I explicitly closed the stream:
BitmapImage imageSource = imagePreview.Source as BitmapImage;
imagePreview.Source = null;
if (null != imageSource)
{
System.IO.Stream stream = imageSource.StreamSource;
imageSource.StreamSource = null;
if (null != stream)
{
stream.Close();
}
// now the file can be deleted
File.Delete(filePath);
}
this solution is working fine. thanks.

Resources