cannot load image in isolated storage when creating IconicTile - windows-phone

I'm trying to create IconicTile using image saved on isolated storage.
At first, I saved image to isolated storage.
// bitmap is Stream of image source (e.g. from PhotoChooserTask)
var wbmp = new WriteableBitmap(bitmap);
var file = "Shared/ShellContent/IconicTile.png";
// when file exists, delete it
if (storage.FileExists(file))
{
storage.DeleteFile(file);
}
using (var isoFileStream = new IsolatedStorageFileStream(file, FileMode.Create, storage))
{
// use ToolStackPNGWriterExtensions
wbmp.WritePNG(isoFileStream);
}
And I have confirmed PNG file is successfully created using Isostorage Explorer Tools.
Then I try to create IconicTile.
var initialData = new IconicTileData
{
BackgroundColor = SelectedColor,
IconImage = new Uri("isostore:/Shared/ShellContent/IconicTile.png", UriKind.Absolute),
Title = tbTitle.Text,
WideContent1 = tbWideContent1.Text,
WideContent2 = tbWideContent2.Text,
WideContent3 = tbWideContent3.Text
};
var uri = string.Format("/SecondaryPage.xaml");
var TileToFind = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains(uri));
if (TileToFind == null)
ShellTile.Create(new Uri(uri, UriKind.Relative), initialData, true);
else
TileToFind.Update(initialData);
But image in the created tile are white.
(I'd like to attach image but my reputation is too low. Sorry.)
I tried not only PNG format but also JPG format, but neither doesn't work.
Is there anyway to create IconicTile using image on IsolatedStorage?

The iconic tile format expects a transparent background, based on your comment at the first line (PhotoChooserTask), I suspect you're using some type of image with no transparency.

Related

ImageMagick.Net - convert pdf to tiff

I am running into an issue when converting from pdf to tiff. Here is the code I used (based on a sample provided in the documentation):
private void convImageMx(string pdfFile)
{
var settings = new MagickReadSettings();
// Settings the density to 300 dpi will create an image with a better quality
settings.Density = new Density(300, 300);
settings.ColorType = ColorType.TrueColor;
string tifpath = Path.GetDirectoryName(pdfFile) + "\\" + Path.GetFileNameWithoutExtension(pdfFile);
using (var images = new MagickImageCollection())
{
// Add all the pages of the pdf file to the collection
images.Read(pdfFile, settings);
var page = 1;
foreach (var image in images)
{
// Write page to file that contains the page number
image.Format = MagickFormat.Ptif;
image.Crop(image.Width, image.Height);
image.Write(tifpath + "_p_" + page + ".tif");
page++;
}
}
}
When I provide a multiple pdf as input, I get multiple tiff files - one file per page. However, each file contains 7 pages which are shrinking images of the original page and the size is very large (original pdf size is 328k, the size of one tiff is 67mb!).
I think I need to set the compression property as well as crop property correctly. But did not find any documentation with .NET.
[EDIT] I commented the line with density so that the size issue is fixed. However, the repeating images is still an issue.

Embed pdf content in pdf layer

I just registered. I try to address the following case:
Given a basic pdf (a simple, single raster image), I want to get to:
Create a pdf (empty initially), in which I create a layer, in which I embed the raster image of the input_pdf, and mark said layer as visible and not_printable.
Are there tools to do it?
Thanks for the tips.
Since you do not specify a language or a library you use, here it is a solution in C#:
// Extract the page content from the source file.
FileStream stream = File.OpenRead("input.pdf");
PDFFile source = new PDFFile(stream);
PDFPageContent pageContent = source.ExtractPageContent(0);
stream.Close();
PDFFixedDocument document = new PDFFixedDocument();
document.OptionalContentProperties = new PDFOptionalContentProperties();
PDFPage page = document.Pages.Add();
// Create an optional content group (layer) for the extracted page content.
PDFOptionalContentGroup ocg = new PDFOptionalContentGroup();
ocg.Name = "Embedded page";
ocg.VisibilityState = PDFOptionalContentGroupVisibilityState.AlwaysVisible;
ocg.PrintState = PDFOptionalContentGroupPrintState.NeverPrint;
// Draw the extracted page content in the layer
page.Canvas.BeginOptionalContentGroup(ocg);
page.Canvas.DrawFormXObject(pageContent, 0, 0, page.Width, page.Height);
page.Canvas.EndOptionalContentGroup();
// Build the display tree for the optional content
PDFOptionalContentDisplayTreeNode ocgNode = new PDFOptionalContentDisplayTreeNode(ocg);
document.OptionalContentProperties.DisplayTree.Nodes.Add(ocgNode);
using (FileStream output = File.Create("EmbedPageAsLayer.pdf"))
{
document.Save(output);
}
The output PDF file is available here: https://github.com/o2solutions/pdf4net/blob/master/GettingStarted/EmbedPageAsLayer/EmbedPageAsLayer.pdf

PNG file is corrupt when reading back from SQL

I have a database program that stores images to the SQL DB and reads them back to be displayed in a WPF application. if i use Jpeg images it works fine, but if i use PNG images, which i wanted to use to try and keep the transparency ( which disappears anyway when stored ) most of the images come back corrupt.
this is the image that has been selected
i then save it to the db, and add the image the listview
then if i close the application and reload it, it pulls the image back from the db, you can see that it is corrupt in the listview
and then when i select it, the image control also shows the corrupted image
i am storing the image in code using a BitmapImage object, and use this to set the image.source, and also convert this to a byte[] for storing into the image field in the database.
i convert the bitmapimage to a Byte[] with the following line
command.Parameters.AddWithValue("#Image", ImageToByteArray(productImage.ProductImage));
and these are the functions to convert to and from a bitmapimage
private static BitmapImage BuildImage(byte[] image)
{
var bitmap = new BitmapImage();
bitmap.BeginInit();
MemoryStream mem = new MemoryStream(image);
bitmap.StreamSource = mem;
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.EndInit();
//bitmap.Freeze();
return bitmap;
}
private static byte[] ImageToByteArray(BitmapImage image)
{
byte[] data;
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(image));
using (MemoryStream ms = new MemoryStream())
{
encoder.Save(ms);
data = ms.ToArray();
}
return data;
}
works fine with Jpegs, but then i have the white background to the images.
any help would be much appreciated.
"JpegBitmapEncoder" is a large clue. Surely you'd want "PNGBitmapEncoder"...?
As personal preference I would change your SQL data column to varbinary(MAX) and use stream and BinaryReader objects to upload the file.
However I think your issue is you are not using PngBitmapEncoder for the PNG you are using the JpegBitmapEncoder irrespective of filetype.
Hope this helps.

Photoshop script how to add image to a layer

How can I grab an image by its location on my local machine and insert it into a layer with extendscript?
var doc = app.documents.add();
var layer = doc.artLayers.add();
var img = new File('~/Desktop/tanks.png');
layer.image = img; //I want to add the image to this layer
All I can seem to do is open the image as a background which creates a new photoshop doc in the process;
var opened = open(img);
But what I would like to achieve is to open multiple images into the same doc as multiple layers. Can this be done?
Open each of the images you want to consolidate using the open method you found. Then cycle through the open document and use the duplicate method on the art layer object to copy all the layers to a single target document. See the code snippet below for copying a single layer image to a new doc.
//copy the layer into the target document
app.activeDocument = pSourceDocument;
pSourceDocument.artLayers[0].duplicate(pTargetDocument);
I found a very useful script for doing this here https://forums.adobe.com/message/3946944#3946944
I took the piece of this script and it worked for me. First of all, you need to convert the layer which contents you want to replace with the image to a Smart Object (in other case contents of the layer can't be replaced by scripts). To do so, open the file you want to modificate in Photoshop, select the layer, click Layer > Smart Objects > Group into New Smart Object. Now this layer is a Smart Object.
Then create a script with the following code:
var replacementFile = new File("X:/file.jpg");
var theLayer = app.documents[0].artLayers.getByName("targetLayer");
theLayer = replaceContents(replacementFile);
////// replace contents //////
function replaceContents (newFile) {
// =======================================================
var idplacedLayerReplaceContents = stringIDToTypeID( "placedLayerReplaceContents" );
var desc3 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
desc3.putPath( idnull, new File( newFile ) );
var idPgNm = charIDToTypeID( "PgNm" );
desc3.putInteger( idPgNm, 1 );
executeAction( idplacedLayerReplaceContents, desc3, DialogModes.NO );
return app.activeDocument.activeLayer
};

Accessing saved image on a Windows Phone

I am creating a camera app for the Windows Phone but when I want to retrieve my saved photo as a BitmapImage, nothing seems to work. I am using IsolatedStorageSettings to save the path of the images as shown below:
Picture p = mediaLibrary.SavePictureToCameraRoll(fileName, e.ImageStream);
if (!settings.Contains("lastImageTaken"))
{
//GetPath() requires using Microsoft.Xna.Framework.Media.PhoneExtensions;
settings.Add("lastImageTaken", p.GetPath());
}
else
{
settings["lastImageTaken"] = p.GetPath();
}
settings.Save();
then once the app starts up, I try to retrieve that last photo taken as shown below:
lastImageTaken = IsolatedStorageSettings.ApplicationSettings["lastImageTaken"] as string;
Uri uri = new System.Uri(lastImageTaken, UriKind.RelativeOrAbsolute);
BitmapImage image = new BitmapImage(uri);
previouseImage.Source = image;
if (image.PixelWidth < 1)
debugText.Text += " FAILED";
I have also tried something like this:
Uri uri = new System.Uri("file:///" + lastImageTaken.Replace("\\", "/"), UriKind.RelativeOrAbsolute);
BitmapImage image = new BitmapImage(uri);
but nothing seems to display the image. The width of to image always is shown as 0 which displays a "FAILED" text on a debug text. lastImageTaken is shown as C:\Data\Users\Public\Camera Roll\SMCA_jpg.jpg
I have also added the capabilities of ID_CAP_MEDIALIB_PHOTO
It seems that you are saving the image to CameraRoll, but trying to retrieve image from IsolatedStorage. Those are two different storage areas, and they are accessed differently.
In order to save an image to IsolatedStorage you need to replace this :
library.SavePictureToCameraRoll(fileName, e.ImageStream);
with this :
using (IsolatedStorageFile isStore = IsolatedStorageFile.GetUserStoreForApplication())
{
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 image to the local folder.
while ((bytesRead = e.ImageStream.Read(readBuffer, 0, readBuffer.Length)) > 0)
{
targetStream.Write(readBuffer, 0, bytesRead);
}
}
}
Then access the image the same way you've described in your original post.
Source : http://msdn.microsoft.com/en-us/library/windows/apps/hh202956(v=vs.105).aspx#BKMK_SavingToTheMediaLibraryAndIsolatedStorage