I'm trying to make a Windows Phone 7 app that will save some images off the web, I have no idea where I can or if I can save images from the web to the phone.
What can I do to save images?
You can download them using WebClient or WebRequest to get the raw bytes.
You can then save to isolated storage fairly easily - but I don't believe you can save in any location seen by the Picture hub, if that's what you were aiming for. (I can't see anything in the Microsoft.Phone.Tasks namespace which would be relevant. There are tasks to capture the camera of choose a photo, but not to save one.)
EDIT: Ooh, I've just found a way. You can use the MediaLibrary class and its SavePicture method... although that's in XNA, so I'd at least have concerns about it working from a Silverlight app. I know some bits of the XNA API do work from Silverlight, and some don't. Worth experimenting with.
On the phone, you can use HttbWebRequest (recommended to avoid UI impact) or WebClient per the project I posted here.
WebClient, HttpWebRequest and the UI Thread on Windows Phone 7
You can then take your stream and pass it into something of this form to write it to isolated storage.
private void PicToIsoStore(Stream pic) {
using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication()) {
var bi = new BitmapImage();
bi.SetSource(pic);
var wb = new WriteableBitmap(bi);
using (var isoFileStream = isoStore.CreateFile("somepic.jpg")) {
var width = wb.PixelWidth;
var height = wb.PixelHeight;
Extensions.SaveJpeg(wb, isoFileStream, width, height, 0, 100);
}
}
}
Jon is correct you can also use MediaLibrary.SavePicture. Be aware that this would put the pics mixed in with the users photos in the Picture Hub.
This is as straight forward as
private void PicToMediaLibary(Stream pic) {
MediaLibrary lib = new MediaLibrary();
lib.SavePicture("blah", pic);
}
Storing it in isolated storage is basically your apps private file system.
Related
I have written a windows store app in XAML & C# to read image from tablet's webcam and decode the barcode using Zxing's lbrary. The code is working fine on a given tablet having an i5 processor while it fails to run on an actual tablet with 2MP camera and "Intel Baytrail Quad-Core" processor.
Any ideas on why this could happen?
Please let me know if you need to see my code for this issue ad I will share.
I am wondering how can the same code work on 1 tablet while fail on another tablet.
Thanks in advance for any help provided.
EDIT
Code used to scan the barcode and read as below - The last if/else block is what I get to. No exception raised :(
string barcodeData = string.Empty;
using (var imageStream = new InMemoryRandomAccessStream())
{
processingImage = true;
var encodingProperties = new ImageEncodingProperties();
encodingProperties.Subtype = "Jpeg";
encodingProperties.Width = 400;
encodingProperties.Height = 400;
await captureMgr.CapturePhotoToStreamAsync(encodingProperties, imageStream);
await imageStream.FlushAsync();
imageStream.Seek(0);
var bitmap = new WriteableBitmap(400, 400);
bitmap.SetSource(imageStream);
preview1.Source = bitmap; //preview1 is an Image control to display the captured image
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(imageStream);
imageStream.Seek(0);
var bitmapDecoder = await BitmapDecoder.CreateAsync(BitmapDecoder.JpegDecoderId, imageStream);
var data = await bitmapDecoder.GetPixelDataAsync(
BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Straight,
new BitmapTransform(),
ExifOrientationMode.IgnoreExifOrientation,
ColorManagementMode.DoNotColorManage
);
if (data != null)
{
BarcodeReader barcodeReader = new BarcodeReader();
var result = barcodeReader.Decode(
data.DetachPixelData(),
(int)bitmapDecoder.PixelWidth,
(int)bitmapDecoder.PixelHeight,
ZXing.RGBLuminanceSource.BitmapFormat.BGR32
);
if (result != null)
{
//Barcode found
}
else
//No data found.
}
}
I guess you are using ZXing.NET library.
Have you ever considered moving to another barcode scanner library?
Accessing the "ISSUES" section in ZXing.NET Library, you can see that there's a lot of bugs still opened for Windows Phone (and should be Window Store also).
http://zxingnet.codeplex.com/workitem/list/basic
One of it called my attention. Check out this comment:
While the WP samples all target Silverlight, you must not forget that the new WP8.1 base is WinRT - so I suggest you use the WinRT sample as a base.
I tried to do the same, but truth to be told, ZXing lacks a lot ATM for WinRT Universal Apps - it's slow, unreliable, and barely ever recognizes a thing.
http://zxingnet.codeplex.com/workitem/13311
I don't know how reliable this is, but the last time the project was updated was on April 7th!!!!
You should really consider changing you library!
Hi,
I made a lib for WinRT using ZXing & Imaging SDK.
It works well (but does not include any additional focus feature).
https://github.com/stepheUp/VideoScanZXing4WP81
There is a lib and a sample app that you can try.
It works for barcodes and QRCode (barcode by default but just change the optional parameter in the scan function code to use QRCode)
Hope it helps,
Stéphanie
In my application I have a data entry view which I'd also like to give the user the option to store a photograph. Currently, using the tutorial at http://msdn.microsoft.com/en-US/library/windowsphone/develop/hh394006(v=vs.105).aspx, I've got a button which lets me take the photo, and can display it back properly at the bottom of the view.
However, I'd like this photo to be saved as part of the record. I thought it would be simple to save the location of the file as a string(I can access this by using PhotoResultObject.OriginalFileName), then display the photo again by using a URI like in my code here. This is inside the cameraCaptureTask_Completed method, e is a PhotoResult object.
string imageLoc = e.OriginalFileName;
Uri imageUri = new Uri(imageLoc, UriKind.RelativeOrAbsolute);
StreamResourceInfo resourceInfo = Application.GetResourceStream(imageUri);
BitmapImage bmp = new BitmapImage();
bmp.SetSource(resourceInfo.Stream);
myImage.Source = bmp;
However, this gives me an Argument exception, or various other exceptions when I move things around. I'm pretty new to c# so I'm really certain what's going on.
I reckon it's something to do with trying to access the photo straight from the phone memory, but I haven't found any other clear ways to store photos which will stay accessible inside the app.
Any tips on where I'm going wrong or how to accomplish my aim would be appreciated greatly!
I am trying to capture MP4 video at a specific resolution in Windows Phone 8 (to be specific, 480x480). I know that I can't use sizes other than the presets, and 480x480 is not a preset. How do I transform a captured video (such as 640x480) and crop the top and bottom to make it 480x480? Any free or open source libraries (that run on Windows Phone) are welcome. Please don't answer with answers such as 'use an external server', I need an on-device solution.
Use the Windows.Phone.Media.Capture APIs and the AudioVideoCaptureDevice class
Second parameter for AudioVideoCaptureDevice.OpenAsync - see this link - is the resolution. And you can get the resolutions using AudioVideoCaptureDevice.GetAvailableCaptureResolutions(sensor)
EDIT: To set custom resolutions try AudioVideoCaptureDevice.SetCaptureResolutionAsync
EDIT 2: You could try something like the following to transform recorded video: (can't find where i got the code from soz to author!)
StorageFolder isoStore = await ApplicationData.Current.LocalFolder.GetFolderAsync("Shared");
var file = await isoStore.CreateFileAsync("foos1.wmv", CreationCollisionOption.ReplaceExisting);
using (var s = await file.OpenAsync(FileAccessMode.ReadWrite))
{
Windows.Foundation.Size resolution = new Windows.Foundation.Size(640, 480);
avDevice = await AudioVideoCaptureDevice.OpenAsync(CameraSensorLocation.Back,
AudioVideoCaptureDevice.GetAvailableCaptureResolutions(CameraSensorLocation.Back).Last());
VideoBrush videoRecorderBrush = new VideoBrush();
videoRecorderBrush.SetSource(avDevice);
viewfinderRectangle.Fill = videoRecorderBrush;
await avDevice.StartRecordingToStreamAsync(s);
Thread.Sleep(30000);
await avDevice.StopRecordingAsync();
}
new MediaPlayerLauncher()
{
Media = new Uri(file.Path, UriKind.Relative),
}.Show();
I need to implement Instagram photo effects like amaro, hudson, sepia, rise, and so on. I know this article only use basic effects: http://code.msdn.microsoft.com/windowsdesktop/Metro-Style-lightweight-24589f50
Another way suggested by people are to implement Direct2d and then apply using that. But for that I would need to write C++ code, where I have zero experience.
Can anyone suggest some other way to implement Instagram effects in c#?
Is there any built in c++ file for these effects?
Please see this example from CodeProject : Metro Style Lightweight Image Processing
The above example contains these image effects.
Negative
Color filter
Emboss
SunLight
Black & White
Brightness
Oilpaint
Tint
Please note above example seems to be implemented on either developer preview or release preview of Windows 8. So you will get error like this
'Windows.UI.Xaml.Media.Imaging.WriteableBitmap' does not contain a
constructor that takes 1 arguments
So you have to create instance of WriteableBitmap by passing pixel height and pixel width of image. I have edited the sample and it is working for me. You have to change wb = new WriteableBitmap(bs); to wb = await GetWB();
StorageFile originalImageFile;
WriteableBitmap cropBmp;
public async Task<WriteableBitmap> GetWB()
{
if (originalImageFile != null)
{
//originalImageFile is the image either loaded from file or captured image.
using (IRandomAccessStream stream = await originalImageFile.OpenReadAsync())
{
BitmapImage bmp = new BitmapImage();
bmp.SetSource(stream);
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
byte[] pixels = await GetPixelData(decoder, Convert.ToUInt32(bmp.PixelWidth), Convert.ToUInt32(bmp.PixelHeight));
cropBmp = new WriteableBitmap(bmp.PixelWidth, bmp.PixelHeight);
Stream pixStream = cropBmp.PixelBuffer.AsStream();
pixStream.Write(pixels, 0, (int)(bmp.PixelWidth * bmp.PixelHeight * 4));
}
}
return cropBmp;
}
Let me know if you are facing any problem.
I want to export an image of my ArcGIS map object with the graphics layer on it. I've tried esri's own web services for export but they're not so efficient and clear, not supporting complex geometric shapes also they're not support local layers such as Google map provider. Service supports only ArcGISTiledLayer i want it in all layers. So, i searched in their forums but they say they won't support local layers until next versions.
I've tried ImageTool libraries and WritableBitmapEx libraries in codeplex. But when i try to get byte[] from a WritableBitmap i can not access its Pixels property for some security reasons all the time. Application throws a SecurityException and says that 'you can't access this pixels property'.
So, is there any way for get a UIElement control's image and save it to the disk? Or is there a workaround for this security exception?
Yes the image tools library has a method to do this into png/jpg etc.
http://imagetools.codeplex.com/
Also you can use RenderTargetBitmap - http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.rendertargetbitmap.aspx
Here is an example of how to save a file to disk. you can only do it from a dialog
http://www.silverlightshow.net/items/Using-the-SaveFileDialog-in-Silverlight-3.aspx
EDIT - Sample Code
Calling
var objImage = new WritableBitmap(MyElement, MyElement.RenderTransform);
var bytData = objImage.ToPng();
Extension Method
using ImageTools.IO.Png;
using ImageTools;
public static byte[] ToPng(this WriteableBitmap Image)
{
byte[] bytResult;
using (MemoryStream objPngStream = new MemoryStream())
{
PngEncoder objPngEncoder = new PngEncoder();
objPngEncoder.Encode(Image.ToImage(), objPngStream);
objPngStream.Seek(0, SeekOrigin.Begin);
bytResult = objPngStream.ToArray();
objPngStream.Close();
}
return bytResult;
}