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
Related
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
I try to retrieve the content of a .gif located on an URL into a string -and eventually save it to disk-, ran from a WPF appliactions using the webbrowser. After trying dozens of solutions I am not further than I was yesterday. I thought that the code below would do the job, but the saved string cGif contains...the url itself. From http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.gifbitmapdecoder.aspx I then thought I needed GifBitmapEncoder instead, but the sample suggests that this creates an empty gif instead of a downloaded one.
(PS: what I basically want to do is to retrieve the GIF bytes straight from the WPF webbrowser but I haven't found anything working there either)
private void GifSave()
{
var uri = new Uri(#"http://www.archivearts.com/GIRAFFE2.gif");
var Img = new GifBitmapDecoder(uri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
string cGif = Img.ToString();
}
Modified from a sample on the GifBitmapDecoder MSDN page:
// Open a Stream and decode a GIF image
var uri = new Uri(#"http://www.archivearts.com/GIRAFFE2.gif");
GifBitmapDecoder decoder = new GifBitmapDecoder(uri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
BitmapSource bitmapSource = decoder.Frames[0];
Then use the CopyPixels() method of the BitmapSource to copy the pixel data into an array. Then convert the array into a string?
I haven't done this, I just took a look at MSDN to get some hints.
This is as far as I've gotten. What code would I write to save a captured image to the server? Without any dialog prompting for a save location. Similar to the way Facebook does it. (I've been unable to find examples online)
void CaptureSource_CaptureImageCompleted(object sender, CaptureImageCompletedEventArgs e)
{
capturedImage.ImageSource = e.Result;
stopCapture(); // turns off webcam
}
It is not that simple.
Create a WCF service on the server.
Consume it on the silverlight client.
Call service method to send an image to the server.
Save it on the server with custom logic.
Or, if this is too complicated - follow this tutorial. It is rather compact RESTful approach demo.
I did i before,
Firstly
ImageTools is good library not must but a good library, you may use.
Beside this you shoul check for permission for camera access. Then here is the code,
Hope helps,
/Capture image part/
_captureSource.CaptureImageCompleted += ((s, args) =>
{
//some other stuffs
domainServiceObject.PR_PATIENTPHOTOs.Clear();
photo = new PR_PATIENTPHOTO();
ImageTools.ExtendedImage eimg=args.Result.ToImage();
var encoder=new ImageTools.IO.Png.PngEncoder();
Stream stream= eimg.ToStreamByExtension("png");
if (stream.Length > 512000)
{
eimg= ExtendedImage.Resize(eimg, 240, new NearestNeighborResizer());
stream = eimg.ToStreamByExtension("png");
}
/Reload Image Part/
//note photo.photo is byte[]
photo = domainServerObject.PR_PATIENTPHOTOs.FirstOrDefault();
if (photo != null)
{
using (MemoryStream ms = new MemoryStream(photo.PHOTO, 0, photo.PHOTO.Length))
{
ms.Write(photo.PHOTO, 0, photo.PHOTO.Length);
BitmapImage img = new BitmapImage();
img.SetSource(ms);
imagePatientPhoto.Source = img;
}
}
I have a very large image (600mb) 30000x30000 and want to load it into a wpf image control.
I can watch this image with the Windows Photo Viewer!
I set my testapp to 64bit and used the following code.
var image = new BitmapImage();
image.BeginInit();
// load into memory and unlock file
image.CacheOption = BitmapCacheOption.OnLoad;
image.UriSource = uri;
image.EndInit();
imagecontrol.source = image;
The test app just shows a white screen with this large image.
Smaller ones like 100mb and 7000x7000 are working.
What am I doing wrong? Sry for my bad english and thanks in advance.
64-Bit Applications.
As with 32-bit Windows operating systems, there is a 2GB limit on the size of an object you can create while running a 64-bit managed application on a 64-bit Windows operating system.
Picture can be fetched directly from the hard drive to reduce a memory usage.
You can use BitmapCacheOption
Code bellow will read the image directly from the HDD and small thumbnail will be generated without the original image cache:
public BitmapImage memoryOptimizedBitmapRead(string url,FrameworkElement imageContainer)
{
if (string.IsNullOrEmpty(url) || imageContainer.ActualHeight<= 0)
{
return null;
}
var bi = new BitmapImage();
bi.BeginInit();
bi.CacheOption = BitmapCacheOption.None;
bi.CreateOptions = BitmapCreateOptions.IgnoreColorProfile;
bi.DecodePixelHeight = (int)imageContainer.ActualHeight;
bi.UriSource = new Uri(url, UriKind.Absolute);
// End initialization.
bi.EndInit();
bi.Freeze();
return bi;
}
Max thumbnail size is determined by the image container size.
Method usage example:
var image= new Image();
image.Source = memoryOptimizedBitmapRead(...);
I'd divide it into 10 (3000x3000) segments and put them into 10 files.
Also check what format you're using it. It may be filling up the threshold for file size or for that particular format. Try TIF format, then try JPG, then try BMP, etc.. Also see if you can compress it with the JPG format to 40-50% and see if that changes anything.
Let me know what you find out.
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);