WPF - Load Font from Stream? - wpf

I have a MemoryStream with the contents of a Font File (.ttf) and I would like to be able to create a FontFamily WPF object from that stream WITHOUT writing the contents of the stream to disk. I know this is possible with a System.Drawing.FontFamily but I cannot find out how to do it with System.Windows.Media.FontFamily.
Note: I will only have the stream, so I can't pack it as a resource in the application and because of disk permissions issues, will not be able to write the font file to disk for reference as "content"
UPDATE:
The API docs how describe how an application resource can be used, though it is not clear to me whether that is an Embedded resource in the assembly or a file on disk.
You can use a base URI value when you reference a font that is packaged as part of the application. For example, the base URI value can be a "pack://application" URI, which lets you reference fonts that are packaged as application resources. The following code example shows a font reference that is composed of a base URI value and a relative URI value.

There is a similar question here, which contains a supposed solution by converting a System.Drawing.FontFamily to a WPF font family, all in memory without any file IO:
public static void Load(MemoryStream stream)
{
byte[] streamData = new byte[stream.Length];
stream.Read(streamData, 0, streamData.Length);
IntPtr data = Marshal.AllocCoTaskMem(streamData.Length); // Very important.
Marshal.Copy(streamData, 0, data, streamData.Length);
PrivateFontCollection pfc = new PrivateFontCollection();
pfc.AddMemoryFont(data, streamData.Length);
MemoryFonts.Add(pfc); // Your own collection of fonts here.
Marshal.FreeCoTaskMem(data); // Very important.
}
public static System.Windows.Media.FontFamily LoadFont(int fontId)
{
if (!Exists(fontId))
{
return null;
}
/*
NOTE:
This is basically how you convert a System.Drawing.FontFamily to System.Windows.Media.FontFamily, using PrivateFontCollection.
*/
return new System.Windows.Media.FontFamily(MemoryFonts[fontId].Families[0].Name);
}
This seems to use the System.Drawing.PrivateFontCollection(^) to add a System.Drawing.Font created from a MemoryStream and then use the Families[0].Name of that font to pass into the System.Windows.Media.FontFamily constructor. I assume the family name would then be a URI to the instance of that font in the PrivateFontCollection but you'd probably have to try it out.

The best approach I could think of, was to save the oldFont to a temp directory, and immediately load it using the newFont constructor that accepts a uri.

Related

Silverlight: Business Application Needs Access To Files To Print and Move

I have the following requirement for a business application:
(All of this could be on local or server)
Allow user to select folder location
Show contents of folder
Print selected items from folder (*.pdf)
Display which files have been printed
Potentially move printed files to new location (sub-folder of printed)
How can I make this happen in Silverlight?
Kind regards,
ribald
First of all, all but the last item can be done (the way you expect). Due to security protocols, silverlight cannot access the user's drive and manipulate it. The closest you can get is accessing silverlight's application storage which will be of no help to you whatsoever in this case. I will highlight how to do the first 4 items.
Allow user to select folder location & Show contents of folder
public void OnSelectPDF(object sender)
{
//create the open file dialog
OpenFileDialog ofg = new OpenFileDialog();
//filter to show only pdf files
ofg.Filter = "PDF Files|*.pdf";
ofg.ShowDialog();
byte[] _import_file = new byte[0];
//once a file is selected proceed
if (!object.ReferenceEquals(ofg.File, null))
{
try
{
fs = ofg.File.OpenRead();
_import_file = new byte[fs.Length];
fs.Read(_import_file, 0, (int)fs.Length);
}
catch (Exception ex)
{
}
finally
{
if (!object.ReferenceEquals(fs, null))
fs.Close();
}
//do stuff with file - such as upload the file to the server
};
}
If you noticed, in my example, once the file is retrieved, i suggest uploading it to a webserver or somewhere with temporary public access. I would recommend doing this via a web service. E.g
//configure the system file (customn class)
TSystemFile objFile = new TNetworkFile().Initialize();
//get the file description from the Open File Dialog (ofg)
objFile.Description = ofg.File.Extension.Contains(".") ? ofg.File.Extension : "." + ofg.File.Extension;
objFile.FileData = _import_file;
objFile.FileName = ofg.File.Name;
//upload the file
MasterService.ToolingInterface.UploadTemporaryFileAsync(objFile);
Once this file is uploaded, on the async result, most likely returning the temporary file name and upload location, I would foward the call to some javascript method in the browser for it to use the generic "download.aspx?fileName=givenFileName" technique to force a download on the users system which would take care of both saving to a new location and printing. Which is what your are seeking.
Example of the javascript technique (remember to include System.Windows.Browser):
public void OnInvokeDownload(string _destination)
{
//call the browser method/jquery method
//(I use constants to centralize the names of the respective browser methods)
try
{
HtmlWindow window = HtmlPage.Window;
//where BM_INVOKE_DOWNLOAD is something like "invokeDownload"
window.Invoke(Constants.TBrowserMethods.BM_INVOKE_DOWNLOAD, new object[] { _destination});
}
catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.ToString()); }
}
Ensure you have the javascript method existing either in an included javaScript file or in the same hosting page as your silverlight app. E.g:
function invokeDownload(_destination) {
//some fancy jquery or just the traditional document.location change here
//open a popup window to http://www.myurl.com/downloads/download.aspx? fileName=_destination
}
The code for download.aspx is outside the scope of my answer, as it varies per need and would just lengthen this post (A LOT MORE). But from what I've given, it will "work" for what you're looking for, but maybe not in exactly the way you expected. However, remember that this is primarily due to silverlight restrictions. What this approach does is rather than forcing you to need a pluging to view pdf files in your app, it allows the user computer to play it's part by using the existing adobe pdf reader. In silverlight, most printing, at least to my knowledge is done my using what you call and "ImageVisual" which is a UIElement. To print a pdf directly from silverlight, you need to either be viewing that PDF in a silverlight control, or ask a web service to render the PDF as an image and then place that image in a control. Only then could you print directly. I presented this approach as a lot more clean and direct approach.
One note - with the temp directory, i would recommend doing a clean up by some timespan of the files on the server side everytime a file is being added. Saves you the work of running some task periodically to check the folder and remove old files. ;)

How to access downloaded sound in IsolatedStorage via URI (WP7)?

I basically downloaded a file name custom.mp3 into my isolatedstorage and I can see it via isolatedstorage explorer....
The question here is... How can I access the particular custom.mp3 via URI?
So far I got this.. but I wonder why it is not working:
alarm.Sound = new Uri("isostore:/custom.mp3", UriKind.Absolute);
Your path is wrong. Nothing else is wrong with your code. Post the code you're using for saving the mp3 file in the first place, if you want further help.
For easier reading, the code to store the MP3 goes something like this..
string alarmfile = "custom.mp3";
isolatedStorageFileStream = new IsolatedStorageFileStream(alarmfile,FileMode.Create,isolatedStorageFile);
long songfilelength = (long) e.Result.Length;
byte[] songbyte = new byte[songfilelength];
e.Result.Read(songbyte, 0, songbyte.Length);
isolatedStorageFileStream.Write(songbyte, 0, songbyte.Length);
isolatedStorageFileStream.Flush();
Only files packaged in the XAML can be used as alarm sound:
Remarks
The Sound URI must point to a file packaged in the application’s .xap
file. Isolated storage is not supported. When the alarm is launched,
the sound is played quietly and then gradually increases in volume.
There is no way to modify this behavior.
From:
Alarm.Sound Property

WPF control hosted in Windows Forms: Is it possible to access resource files?

I have a WPF control hosted in Windows Forms and I would like to access it's resources, specifically images. What is the best way to do that?
I was thinking of using a ResourceDictionary, but I'm not sure how I can access it from within a Windows Form.
This is a standalone WPF control, in a DLL? There are two ways that the resources can be embedded... as part of a .resources file (e.g. the Project's "Resoruces" tab), or as files included as "Embedded Resources"...
To get the assembly reference to the DLL this is usually the easiest:
var ass = typeof(ClassInOtherDll).Assembly;
In the Resources
If this is part of the default project resources, and not a different .resources file included inside the DLL, you can use the default base-name.
var ass = typeof(ClassInTargetDLL).Assembly;
var rm = new ResourceManager("...BaseName...", ass);
BaseName for default project resources is :
C# := Namespace.Properties.Resources
VB := Namespace.Resources
After that you can just call GetObject() and do a cast back:
var myImage = rm.GetObject("check_16");
return myImage as Bitmap;
If you want to find out whats in there, get the assembly reference and call
ass.GetManifestResourceNames()
.resources files can be used with a ResourceManager
embedded resources will just show up as a list. Use the other method for these.
And this is all assuming they default culture =)
As Embedded Resources
You can use the regular methods on the assembly to get an embedded resource. You find it by name, and then Basically you will need to convert the stream back into your desired type.
GetManifestResourceNames()
GetManifestResourceStream(name)
help getting the desired file with a helper function to find files by name.
I usually use these like this:
// called GetMp3 in the post, but it returns a stream. is the same thing
var stream = GetResourceStream("navigation.xml");
var reader = New XmlTextReader(stream);
return reader;
To get an image that is an embedded resource, this works for me:
var stream = GetResoureStram("check_32.png");
var bmp = new Bitmap(stream);
this.MyButton.Image = bmp;

Loading BitmapImage in code

From my assembly (A) I want to call a method in another assembly (B) which passes an image. This image is then shown in a WPF Window - the window is part of B's project.
I can't seem to pass an ImageSource with a pack:// uri as this gets evaluated in the context of B, so I guess I need to cache the image using CachedBitmap (?) when still in A.
BitmapImage img = new BitmapImage(new Uri("Images/32px-Nuvola_apps_cache.png", UriKind.Relative));
CachedBitmap cbmp = new CachedBitmap(img, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
I've managed to get this to work if I set the image to Embedded Resource and load it as a stream, but this isn't the WPF way.
It seems from the pack: documentation that I should be able to do this, but I've tried these below and none work;
"Images/32px-Nuvola_apps_cache.png": "Could not find part of the path"
"pack://application:,,,Images/32px-Nuvola_apps_cache.png": "The URI prefix is not recognized.".
"pack://siteoforigin:,,,Images/32px-Nuvola_apps_cache.png": "The URI prefix is not recognized."
All I want to do is load a Resource .png file into memory and pass it wholesale to a method in another assembly.
Thanks
Paul.
Try:
pack://application:,,,/YourAssemblyName;component/Images/32px-Nuvola_apps_cache.png

Load image not in xap Silverlight

i'm developing an application and i would load an image that isn't in the clientbin folder, but in a folder placed in my server. I would do something like this
BitmapImage bit = new BitmapImage();
string path = "c:/image.png";
bit.UriSource = new Uri(path, UriKind.Absolute);
identity.Source = bit;
but it doesn't function.Any idea?
Thanks
Try:
Image.Source = New Imaging.BitmapImage(New Uri("http://www.Pic.jpg", UriKind.Absolute))
You do not want to include it in your project or the .xap will get huge.
Silverlight applications run on the client, not on the server, so referencing the path as you had been trying to do, would really point to the path on client machine running the application. However, Silverlight does not have rights to read or write from or to the client's disk anyway, due to sercurity reasons. The Isolated Storage functionality is the exception to that.
Therefore, your option is to try the first answer given by Bill, or a WebService, which is more complex to implement.
ib.
To add to Bill's answer. You can also use server side resources using the following helper method which is derived from the location of the xap file.
public static string GetUrlForResource(string resourcePage)
{
var webUrl = Application.Current.Host.Source.ToString();
//Get ClientBin Directory
var stub = webUrl.Substring(0, webUrl.LastIndexOf("/"));
//Get application root.
stub = stub.Substring(0, stub.LastIndexOf("/") + 1);
//Append the application root to the resource page.
webUrl = stub + resourcePage;
return webUrl;
}
To use it like Bill's answer, use the following:
Image.Source = new Imaging.BitmapImage(new Uri(GetUrlForResource("images/myimage.png"), UriKind.Absolute));

Resources