save image into isolated storage [duplicate] - windows-phone-7

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
store image into isolated storage in windows phone 7
I am using Visual Studio/Expression Blend to create my app for windows phone 7. The user should be able to select a picture that he/she wants to edit and after editing, the user can click a "save" button and the specific edited image will be saved in isolated storage. But I'm having trouble saving the image to Isolated Storage from the button click event.
Does anyone have a code example of how this can be achieved? Thanks!
My codes for the button :
using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
var bi = new BitmapImage(); bi.SetSource(pic);
var wb = new WriteableBitmap(lion.jpg,lion.jpg.RenderTransform);
using (var isoFileStream = isoStore.CreateFile("somepic.jpg"))
{
var width = wb.PixelWidth;
var height = wb.PixelHeight;
Extensions.SaveJpeg(wb, isoFileStream, width, height, 0, 100);
}
}

To save an image to IsolatedStorage from a PhotoChooserTask, use this (the e object in the task callback holds the stream):
public static void SaveImage(Stream imageStream, string fileName, int orientation, int quality)
{
using (var isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
if (isolatedStorage.FileExists(fileName))
isolatedStorage.DeleteFile(fileName);
IsolatedStorageFileStream fileStream = isolatedStorage.CreateFile(fileName);
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(imageStream);
WriteableBitmap wb = new WriteableBitmap(bitmap);
wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, orientation, quality);
fileStream.Close();
}
}

Related

How to load/draw an image using NGraphics?

In my Xamarin Forms app, I have an image under androidProject/Resources/drawable/myImage.png. To load these from Xamarin, you can simply do
Image myImage = new Image() { Source = ImageSource.FromFile("myImage.png") };
However, there is no way to draw an Image using NGraphics. Instead, NGraphics DrawImage(IImage) requires an IImage. As far as I can tell, there's no way to turn a Xamarin.Forms.Image into an NGraphics.IImage. In fact, the only way I could find to load IImage is
IImage myImage = Platform.LoadImage("myImage.png");
However, this doesn't work because under the hood this uses BitmapFactory.decodeFile(), which requires the absolute file path. And I couldn't find any way to get the absolute file path of a resource (if it even exists?)
So, how do I actually load and display an image using NGraphics?
NGraphics does not provide any helpers to load images from your Platforms Resource files.
You could do something as follows. However, it will add some overhead converting back and forth between bitmap -> stream -> bitmap.
Android:
Stream GetDrawableStream(Context context, int resourceId)
{
var drawable = ResourcesCompat.GetDrawable(context.Resources, resourceId, context.Theme);
if (drawable is BitmapDrawable bitmapDrawable)
{
var stream = new MemoryStream();
var bitmap = bitmapDrawable.Bitmap;
bitmap.Compress(Bitmap.CompressFormat.Png, 80, stream);
bitmap.Recycle();
return stream;
}
return null;
}
iOS:
Stream GetImageStream(string fileName)
{
using (var image = UIImage.FromFile(fileName))
using (var imageData = image.AsPNG())
{
var byteArray = new byte[imageData.Length];
System.Runtime.InteropServices.Marshal.Copy(imageData.Bytes, byteArray, 0, Convert.ToInt32(imageData.Length));
var stream = new MemoryStream(byteArray);
return stream;
}
return null;
}
However, you could go directly from Bitmap to BitmapImage on Android instead like:
BitmapImage GetBitmapFromDrawable(Context context, int resourceId)
{
var drawable = ResourcesCompat.GetDrawable(context.Resources, resourceId, context.Theme);
if (drawable is BitmapDrawable bitmapDrawable)
{
var bitmap = bitmapDrawable.Bitmap;
return new BitmapImage(bitmap);
}
return null;
}
And on iOS:
CGImageImage GetImageStream(string fileName)
{
var iOSimage = UIImage.FromFile(fileName);
var cgImage = new CGImageImage(iOSImage.CGImage, iOSImage.Scale);
return cgImage;
}
BitmapImage and CGImageImage implement IImage in NGraphics.

Structure of SKPictureRecorder

I am trying to use SKPictureRecorder of SkiaSharp to draw a path in the SKCanvas. The image is not saved in the disk. Please find the code below for your reference.
SKPictureRecorder record = new SKPictureRecorder();
SKCanvas tempCanvas= record.BeginRecording(new SKRect(0,0,640,480));
tempCanvas.Clear(SKColors.White);
// set up drawing tools
using (var paint = new SKPaint())
{
paint.IsAntialias = true;
paint.Color = new SKColor(0x2c, 0x3e, 0x50);
paint.StrokeCap = SKStrokeCap.Round;
// create the Xamagon path
using (var path = new SKPath())
{
path.MoveTo(71.4311121f, 56f);
path.CubicTo(68.6763107f, 56.0058575f, 65.9796704f, 57.5737917f, 64.5928855f, 59.965729f);
path.LineTo(43.0238921f, 97.5342563f);
path.CubicTo(41.6587026f, 99.9325978f, 41.6587026f, 103.067402f, 43.0238921f, 105.465744f);
path.LineTo(64.5928855f, 143.034271f);
path.CubicTo(65.9798162f, 145.426228f, 68.6763107f, 146.994582f, 71.4311121f, 147f);
path.LineTo(114.568946f, 147f);
path.CubicTo(117.323748f, 146.994143f, 120.020241f, 145.426228f, 121.407172f, 143.034271f);
path.LineTo(142.976161f, 105.465744f);
path.CubicTo(144.34135f, 103.067402f, 144.341209f, 99.9325978f, 142.976161f, 97.5342563f);
path.LineTo(121.407172f, 59.965729f);
path.CubicTo(120.020241f, 57.5737917f, 117.323748f, 56.0054182f, 114.568946f, 56f);
path.LineTo(71.4311121f, 56f);
path.Close();
// draw the Xamagon path
tempCanvas.DrawPath(path, paint);
}
}
SKPicture picture= record.EndRecording();
using (var image = SKImage.FromPicture(picture, new SKSizeI(640, 480)))
using (var data = image.Encode(SKEncodedImageFormat.Png, 80))
{
// save the data to a stream
using (var stream = File.OpenWrite("testing.png"))
{
data.SaveTo(stream);
}
}
Can you check any issue with the code?
Also please let us me know the structure of SKPictureRecorder of SkiaSharp.
Thanks in advance.
Regards,
Sabari

Screenshot in windows phone 7

Can anyone please tell me how to take screenshot and save it to the folder programmatically in windows phone 7.I don't want to save images in the MediaLibrary(),but i want to save it into the folder which is in the root directory of the application
This code will help you to take screenshot against an AppBar button click action. However you have to modify the code to save screenshot into root folder of the Application. The following links will help you.
Save into local storage
Data for Windows Phone
private void ApplicationBarScreenshotButton_Click(object sender, EventArgs e)
{
var fileName = String.Format("MyImage_{0:}.jpg", DateTime.Now.Ticks);
WriteableBitmap bmpCurrentScreenImage = new WriteableBitmap((int)this.ActualWidth, (int)this.ActualHeight);
bmpCurrentScreenImage.Render(LayoutRoot, new MatrixTransform());
bmpCurrentScreenImage.Invalidate();
SaveToMediaLibrary(bmpCurrentScreenImage, fileName, 100);
MessageBox.Show("Captured image " + fileName + " Saved Sucessfully", "WP Capture Screen", MessageBoxButton.OK);
currentFileName = fileName;
}
public void SaveToMediaLibrary(WriteableBitmap bitmap, string name, int quality)
{
using (var stream = new MemoryStream())
{
// Save the picture to the Windows Phone media library.
bitmap.SaveJpeg(stream, bitmap.PixelWidth, bitmap.PixelHeight, 0, quality);
stream.Seek(0, SeekOrigin.Begin);
// Use Above link to store file into Root folder.
//new MediaLibrary().SavePicture(name, stream);
}
}

nokia Imaging SDK customize BlendFilter

I have created this code
Uri _blendImageUri = new Uri(#"Assets/1.png", UriKind.Relative);
var _blendImageProvider = new StreamImageSource((System.Windows.Application.GetResourceStream(_blendImageUri).Stream));
var bf = new BlendFilter(_blendImageProvider);
Filter work nice. But I want change image size for ForegroundSource property. How can I load image with my size?
If I understood you correctly you are trying to blend ForegroundSource with only a part of the original image? That is called local blending at it is currently not supported on the BlendFilter itself.
You can however use ReframingFilter to reframe the ForegroundSource and then blend it. Your chain will look like something like this:
using (var mainImage = new StreamImageSource(...))
using (var filterEffect = new FilterEffect(mainImage))
{
using (var secondaryImage = new StreamImageSource(...))
using (var secondaryFilterEffect = new FilterEffect(secondaryImage))
using (var reframing = new ReframingFilter(new Rect(0, 0, 500, 500), 0)) //reframe your image, thus "setting" the location and size of the content when blending
{
secondaryFilterEffect.Filters = new [] { reframing };
using (var blendFilter = new BlendFilter(secondaryFilterEffect)
using (var renderer = new JpegRenderer(filterEffect))
{
filterEffect.Filters = new [] { blendFilter };
await renderer.RenderAsync();
}
}
}
As you can see, you can use the reframing filter to position the content of your ForegroundSource so that it will only blend locally. Note that when reframeing you can set the borders outside of the image location (for example new Rect(-100, -100, 500, 500)) and the areas outside of the image will appear as black transparent areas - exactly what you need in BlendFilter.

Can I update a live tile in Mango using local data?

I have a Mango WP7.5 app that uses a local SqlCe database. I would like to add a LiveTile update that shows info taken from the local DB based on current day and month.
All the samples that I've found update the background by downloading remote images from servers but I would simply need to make a local database query and show a string in my tile.
Can I do it? How?
Yes, you can. You have to
generate an image containing your textual information
save this image to isolated storage and
access it via isostore URI.
Here is code showing how to do this (it updates the Application Tile):
// set properties of the Application Tile
private void button1_Click(object sender, RoutedEventArgs e)
{
// Application Tile is always the first Tile, even if it is not pinned to Start
ShellTile TileToFind = ShellTile.ActiveTiles.First();
// Application Tile should always be found
if (TileToFind != null)
{
// create bitmap to write text to
WriteableBitmap wbmp = new WriteableBitmap(173, 173);
TextBlock text = new TextBlock() { FontSize = (double)Resources["PhoneFontSizeExtraLarge"], Foreground = new SolidColorBrush(Colors.White) };
// your text from database goes here:
text.Text = "Hello\nWorld";
wbmp.Render(text, new TranslateTransform() { Y = 20 });
wbmp.Invalidate();
// save image to isolated storage
using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
// use of "/Shared/ShellContent/" folder is mandatory!
using (IsolatedStorageFileStream imageStream = new IsolatedStorageFileStream("/Shared/ShellContent/MyImage.jpg", System.IO.FileMode.Create, isf))
{
wbmp.SaveJpeg(imageStream, wbmp.PixelWidth, wbmp.PixelHeight, 0, 100);
}
}
StandardTileData NewTileData = new StandardTileData
{
Title = "Title",
// reference saved image via isostore URI
BackgroundImage = new Uri("isostore:/Shared/ShellContent/MyImage.jpg", UriKind.Absolute),
};
// update the Application Tile
TileToFind.Update(NewTileData);
}
}

Resources