i am trying to save an image i.e., present in my project to the isolated storage. i tried using the following code but it is throwing System.NullReferenceException.
IsolatedStorageFileStream fileStream1 = myIsolatedStorage.CreateFile("shared/shellcontent/image.jpg");
Uri uri = new Uri(#"/myapp;/component/contact.bmp", UriKind.Relative);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.UriSource = uri;
WriteableBitmap wb = new WriteableBitmap(bitmapImage); //throwing exception
Extensions.SaveJpeg(wb, fileStream1, wb.PixelWidth, wb.PixelHeight, 0, 85);
i tried with jpg image also. copy to output directory set to copy if never. also tried with build action set to content
.............
just tried this code and the exception isn't showing now but still when am setting it to a tile background. its blank
Uri uri = new Uri("contact.jpg", UriKind.Relative);
StreamResourceInfo sri = null;
sri = Application.GetResourceStream(uri);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(sri.Stream);
WriteableBitmap wb = new WriteableBitmap(bitmapImage);
Extensions.SaveJpeg(wb, fileStream1, wb.PixelWidth, wb.PixelHeight, 0, 85);
update:
issue is resolved. filestream was not closed. silly me
Related
I'm new to LibGDX and I am taking it slowly. When I run the application it just force closes due to an error. But I'm not sure what the problem is. Here is the code for the button that I have.
#Override
public void show() {
// Viewport Camera
camera = new PerspectiveCamera();
viewport = new ExtendViewport(800, 480, camera);
stage = new Stage(new ExtendViewport(800, 840));
Gdx.input.setInputProcessor(stage);
skin = new Skin(atlas);
atlas = new TextureAtlas(Gdx.files.internal("ui/button.pack"));
table = new Table(skin);
table.setBounds(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
textButtonStyle = new TextButtonStyle();
textButtonStyle.up = skin.getDrawable("button.up");
textButtonStyle.down = skin.getDrawable("button.down");
buttonPlay = new TextButton("Play", textButtonStyle);
buttonPlay.setWidth((float) (Gdx.graphics.getWidth()/2.5));
buttonPlay.setHeight(Gdx.graphics.getHeight()/6);
buttonPlay.setPosition((Gdx.graphics.getWidth()/2-buttonPlay.getWidth()/2), (float) (Gdx.graphics.getHeight()-(buttonPlay.getHeight())*2.5));
buttonPlay.toFront();
stage.addActor(buttonPlay);
table.debug();
table.add(buttonPlay);
}
The error:
Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.badlogic.gdx.scenes.scene2d.ui.Skin.addRegions(Skin.java:102)
at com.badlogic.gdx.scenes.scene2d.ui.Skin.<init>(Skin.java:88)
at com.badlogic.gdx.screens.Menu.show(Menu.java:83)
at com.badlogic.gdx.Game.setScreen(Game.java:61)
at com.badlogic.gdx.screens.Splash.render(Splash.java:38)
at com.badlogic.gdx.Game.render(Game.java:46)
at com.dakotapederson.slingshotsteve.SlingshotSteve.render(SlingshotSteve.java:29)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:206)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
Line 75 is where the show() method is.
Your problem is that you are initialising your skin with an atlas before your atlas has been initialised, the atlas is the null pointer. You have done:
skin = new Skin(atlas); //Here atlas isn't loaded so exception occurs
atlas = new TextureAtlas(Gdx.files.internal("ui/button.pack"));
When it should be:
atlas = new TextureAtlas(Gdx.files.internal("ui/button.pack")); // Load atlas first
skin = new Skin(atlas);
I'm developing a WP8 app using Nokia Imaging SDK.
I'm trying to add filter effect to an image and render it into a WriteableBitmap.
Here is my code:
private async void PhotoChosen(object sender, PhotoResult photoResult)
{
if (photoResult != null)
{
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(photoResult.ChosenPhoto);
WriteableBitmap wb = new WriteableBitmap(bitmap.PixelWidth, bitmap.PixelHeight);
StreamImageSource source = new StreamImageSource(photoResult.ChosenPhoto);
var effects = new FilterEffect(source);
effects.Filters = new IFilter[] { new SketchFilter() };
var renderer = new WriteableBitmapRenderer(effects, wb);
await renderer.RenderAsync();
}
}
All is going fine, but when this line is processing:
await renderer.RenderAsync();
This ArgumentException is thrown:
Value does not fall within the expected range
I think I've made a mistake creating the IImageProvider effects or the WriteableBitmap wb
Does anyone got this problem and found an issue ?
Thanks :)
You need to set the stream position before setting it as source for StreamImageSource.
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(photoResult.ChosenPhoto);
WriteableBitmap wb = new WriteableBitmap(bitmap.PixelWidth, bitmap.PixelHeight);
photoResult.ChosenPhoto.Position = 0;
StreamImageSource source = new StreamImageSource(photoResult.ChosenPhoto);
You need to do this because you have called bitmap.SetSource(photoResult.ChosenPhoto). That means that the stream has already been read once, therefore it's position is at the very end of the stream. When StreamImageSource tries to read it, it is already at the end, thus "Value does not fall within the expected range."
How can I convert a WriteableBitmap to stream on WP8 platform?
Final goal is to dump the image output produced by native code to the CameraRoll library using SavePictureToCameraRoll(filename, stream)
You can encode your bitmap to stream with WritableBitmap.SaveJpeg method and use this stream as a parameter to MediaLibrary.SavePictureToCameraRoll.
Note: before calling MediaLibrary.SavePictureToCameraRoll don't forget to set the stream position to 0 if you're using a MemoryStream. Like this:
var wb = new WriteableBitmap(bitmap);
var fileStream = new MemoryStream();
wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, 100, 100);
fileStream.Seek(0, SeekOrigin.Begin);
var m = new MediaLibrary();
m.SavePictureToCameraRoll("test", fileStream);
I have stored few images in isolatedstorage and I am trying to replace them by using
using (IsolatedStorageFile isStore = IsolatedStorageFile.GetUserStoreForApplication()){
if (isStore.FileExists(fileName)){
isStore.DeleteFile(fileName);
}
using (IsolatedStorageFileStream targetStream = isStore.OpenFile(fileName, FileMode.Create, FileAccess.Write)){
// Initialize the buffer for 4KB disk pages.
byte[] readBuffer = new byte[4096];
int bytesRead = -1;
// Copy the thumbnail to the local folder.
while ((bytesRead = e.ImageStream.Read(readBuffer, 0, readBuffer.Length)) > 0){
targetStream.Write(readBuffer, 0, bytesRead);
targetStream.Close();
}
}
Now When I am trying to access the new file I end up seeing the old photo. the new photo is not replaced immediately.
But when I close the app and again fetch it I get the new photo. What is wrong?
I was using an ImageBrush to paint the background grid and was binding only the ImageSource of the ImageBrush. I guess this ImageBrush was not getting updated so instead of changing the source of the ImageBrush I created a new object of it and assigned it to Grid.Background. so now it works :)
I'm trying to save an image from the web to the isolated storage from a background task but it throws
An unhandled exception of type 'System.UnauthorizedAccessException' occurred in System.Windows.ni.dll
i'm using this piece of code
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
if (myIsolatedStorage.FileExists(tempJPEG))
{
myIsolatedStorage.DeleteFile(tempJPEG);
}
IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(tempJPEG);
StreamResourceInfo sri = null;
Uri uri = new Uri(tempJPEG, UriKind.Relative);
sri = Application.GetResourceStream(uri);
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(e.Result);
WriteableBitmap wb = new WriteableBitmap(bitmap);
// Encode WriteableBitmap object to a JPEG stream.
Extensions.SaveJpeg(wb, fileStream, wb.PixelWidth, wb.PixelHeight, 0, 100);
//wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
fileStream.Close();
}
This works 100% when it's runing from the app , but not from the background task.
Any tips on how to save the image ?
You need to invoke this code Dispatcher.BeginInvoke() since WriteableBitmap needs to execute on the UI thread and not a background thread. See # http://codeblog.vurdalakov.net/2012/02/solution-wp7-unauthorizedaccessexceptio.html