locked
How to get uri for the resource dynamically created in memory RRS feed

  • Question

  • Hello.

    My application creates fonts. 
    Can I get URI for the resource (font) which was created in memory, without saving it into a temporary file ?

     

    Monday, April 28, 2008 10:44 AM

Answers

  •  

    Thank you,

    But how to bind WMI with my application...

     

    Here is my task:

    I have :

    Code Snippet

    FixedPage MyPage;

    Byte[] Custom_Font;

     

     

     

    Then I add Glyphs objects to the page

     

    Code Snippet

    TextObj = new Glyphs();

    TextObj.UnicodeString = " CustomFont Text 1";

    TextObj.FontUri = Create_Memory_Resource_Uri(Custom_Font);

    MyPage.Children.Add(TextObj);

     

     

     

     

    when I created this branch I have:

     

    Code Snippet

    Uri Create_Memory_Resource_Uri (byte[] in_memory_resource)

    {

    MemoryStream resourceStream = new MemoryStream(in_memory_resource);

    string tempPath =

       System.IO.Path.GetTempPath() + (TempFileInd++).ToString();

    FileStream tempStream =

       new FileStream(tempPath,FileMode.Create,FileAccess.Write);

         CopyStream(resourceStream, tempStream);

         tempStream.Close();

         return new Uri(tempPath);

    }

     

     

    it works but I do not like a lot of temporary files
     
     

    Now I have found the solution

     

    Code Snippet

    Uri Create_Memory_Resource_Uri (byte[] in_memory_resource,int Size)

    {

    MemoryStream packStream = new MemoryStream();

         Package pack = 

    Package.Open(packStream,FileMode.Create,FileAccess.ReadWrite);

         Uri packUri = new Uri("AnyBeforeColon:");

         PackageStore.AddPackage(packUri, pack);

         Uri packPartUri = new Uri("/AnyAfterSlash", UriKind.Relative);

       //PackagePart packPart = 

       //   pack.CreatePart(packPartUri, "application/vnd.ms-opentype");

         PackagePart packPart =

           pack.CreatePart(packPartUri, "AnyBeforeSlash/AnyAfterSlash");

       //MemoryStream resourceStream = new MemoryStream(in_memory_resource);

       //CopyStream(resourceStream, packPart.GetStream());

         packPart.GetStream().Write(in_memory_resource, 0, Size);

         Uri memory_resource_uri =

             PackUriHelper.Create(packUri, packPart.Uri);

         return memory_resource_uri;

     

     

    It works, and I think it is enough.
    Monday, May 5, 2008 12:01 PM

All replies

  • Evgenij,

     

    According to your question on getting URI for the in-memory font resource, I would like to provide you the article below and hope that can relate to your question:

     

    Windows Remote Management and WMI

     

    Generating a Complete URI for WMI Classes

    The URI that you supply, either to the Winrm command-line tool or to a script, consists of the prefix plus the resource specification.

    The following procedure describes how to generate a resource URI either to get a WMI class or to use in an enumerate operation.

     

    To generate a resource URI for a WMI class

    1. Start with the prefix that indicates the WS-Management protocol schema should be used.

      http://schemas.microsoft.com/wbem/wsman/1

      The resource URI prefix for WMI classes is always the same. For more information, see URI Prefixes.

    2. Add the WMI namespace to the prefix.

      http://schemas.microsoft.com/wbem/wsman/1/wmi/root/cimv2/

    3. Add the class name.

      http://schemas.microsoft.com/wbem/wsman/1/wmi/root/cimv2/Win32_Service

    4. To set the value of a property, or to invoke a specific method, add the required key value or values for the class.

      http://schemas.microsoft.com/wbem/wsman/1/wmi/root/cimv2/Win32_Service?Name=Winmgmt

    Thursday, May 1, 2008 7:45 AM
  •  

    Thank you,

    But how to bind WMI with my application...

     

    Here is my task:

    I have :

    Code Snippet

    FixedPage MyPage;

    Byte[] Custom_Font;

     

     

     

    Then I add Glyphs objects to the page

     

    Code Snippet

    TextObj = new Glyphs();

    TextObj.UnicodeString = " CustomFont Text 1";

    TextObj.FontUri = Create_Memory_Resource_Uri(Custom_Font);

    MyPage.Children.Add(TextObj);

     

     

     

     

    when I created this branch I have:

     

    Code Snippet

    Uri Create_Memory_Resource_Uri (byte[] in_memory_resource)

    {

    MemoryStream resourceStream = new MemoryStream(in_memory_resource);

    string tempPath =

       System.IO.Path.GetTempPath() + (TempFileInd++).ToString();

    FileStream tempStream =

       new FileStream(tempPath,FileMode.Create,FileAccess.Write);

         CopyStream(resourceStream, tempStream);

         tempStream.Close();

         return new Uri(tempPath);

    }

     

     

    it works but I do not like a lot of temporary files
     
     

    Now I have found the solution

     

    Code Snippet

    Uri Create_Memory_Resource_Uri (byte[] in_memory_resource,int Size)

    {

    MemoryStream packStream = new MemoryStream();

         Package pack = 

    Package.Open(packStream,FileMode.Create,FileAccess.ReadWrite);

         Uri packUri = new Uri("AnyBeforeColon:");

         PackageStore.AddPackage(packUri, pack);

         Uri packPartUri = new Uri("/AnyAfterSlash", UriKind.Relative);

       //PackagePart packPart = 

       //   pack.CreatePart(packPartUri, "application/vnd.ms-opentype");

         PackagePart packPart =

           pack.CreatePart(packPartUri, "AnyBeforeSlash/AnyAfterSlash");

       //MemoryStream resourceStream = new MemoryStream(in_memory_resource);

       //CopyStream(resourceStream, packPart.GetStream());

         packPart.GetStream().Write(in_memory_resource, 0, Size);

         Uri memory_resource_uri =

             PackUriHelper.Create(packUri, packPart.Uri);

         return memory_resource_uri;

     

     

    It works, and I think it is enough.
    Monday, May 5, 2008 12:01 PM
  •  

    Hi, I've been trying to get this to work all day without success. Could you please post e complete working example, it would be sooo great! I 'm new to using Package,I guess that's where ot fails.

     

    Friday, May 9, 2008 12:54 PM
  • I can not find how to attach code samle Use this link to download: http://www.evmgames.com/download/fontembed.rar
    Monday, May 26, 2008 12:29 PM
  • @Evgenij

    Do you know why a FontFamily created from PackageStore Uri wouldn't load properly?

    It works fine when you set the myGlyphs.FontUri = myCreatedFontUri

    but

    myTextBlock.FontFamily = new FontFamily(myCreatedFontUri, "TheNameOfMyCustomFont")

    doesn't work.
    Thursday, September 18, 2008 1:19 AM
  • May be your custom font was not installed in system.
    But I am not sure we can install fonts any other way except AddFontMemResource.
    Tuesday, September 23, 2008 6:41 AM