Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6818713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:12:46+00:00 2026-05-26T21:12:46+00:00

We have pretty much C# 2.0 code that heavily relies on System.Drawing namespace. Also

  • 0

We have pretty much C# 2.0 code that heavily relies on System.Drawing namespace. Also there is some WinGDI dependencies (via interop).

How would you recommend to tackle the problem of making functionally equivalent Silverlight version of the code? We want to reuse code as much as possible because we want to continue develop both versions of the code.

Maybe there is some articles/books you could recommend?

UPDATE: The code is a non-visual component. Not an application. There is no 3rd party dependencies.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:12:46+00:00Added an answer on May 26, 2026 at 9:12 pm

    First of all, you should accept that some of the features of the original code won’t make it to Silverlight version. Interop, reading and writing files – such things are either forbidden because of security reasons or just unsupported.

    Second thing to have in mind is that your code will be polluted with conditional compilation (in case you want to continue to support building of .NET version from the same code base).

    And the third thing is – you will have to write some new code (instead of removed code). For example, you might need to create new methods that accept WriteableBitmap instead of methods that accept System.Drawing.Bitmap in order to give users of Silverlight version similar set of features.

    Ok, let’s take a look at what you might need to do in order to create Silverlight version of a .NET library.

    1. Create new project for Silverlight version
    2. Add all existing code files to this project as links.
    3. Try to build the project. Most probably the build will fail with many warning and error messages. Obviously, the goal is to fix all of them.

    Here is some hints for what can be done to fix common build errors.

    1. Remove all using namespace-name directives that are not needed. Exclude unsupported namespaces with conditional compilation like so:
    #if !SILVERLIGHT
        using System.Drawing;
    #endif
    
    1. If you are using enumerations that are missing in Silverlight (e.g. System.Drawing.Imaging.ImageFormat) then introduce equvalent custom enumerations (e.g. MyImageFormat) and change internal code to use only custom enumerations. Add overloaded methods that use custom enumerations (or equivalent Silverlight enumerations) to public interface, if needed.

    2. Do likewise for the structs (e.g. System.Drawing.PointF) or change the code to use simpler types (e.g. two floats instead of PointF structure)

    3. Exclude public and private code that uses unsupported structures with conditional compilation. Consider rewriting internal code so it will use only language constructs supported in .NET and Silverlight.

    4. Create a wrapper class for accessing embedded resources in Silverlight version because there won’t be any ready-made wrappers that give you byte[] or string for binary and text resources.

    5. Create a property like this

        public static Encoding DefaultEncoding
        {
            get
            {
        #if SILVERLIGHT
                return Encoding.UTF8;
        #else
                return Encoding.Default;
        #endif
            }
        }
    

    And use this property instead of Encoding.Default in your code.

    Sooner or later you will be able to create Silverlight version of your code. This version will probably have less features but, hey, Silverlight is not a full-blown .NET. Some features will even be unnecessary in Silverlight. For some original features you might later add equivalent ones.

    If you are using nunit for unit-testing your .NET version then you might want to take a look at nunit-silverlight (check this page too) for testing Silverlight version. There are some caveats, though.

    1. TestCaseSource attribute is unsupported by nunit-silverlight.
    2. Reading of local files is unsupported.

    If you need to read or write local files in your tests then you should use Silverlight 4 for your test application. There is no way to do this in Silverlight 3. You should also setup your test application as Out-of-Browser one and give it elevated trust rights (check “require elevated trust” in Out-of-Browser settings)

    You’ll need a wrapper (yeah, another one) for reading and writing local files, because Silverlight tests will be able to consume and produce only byte buffers and streams.

    Here are some code snippets that might be useful for the wrapper:

    Get the path to current folder:

    Uri uri = new Uri(System.Windows.Application.Current.Host.Source, relativeFileName);
    var currentPath = uri.OriginalString;
    

    Please note, that you’ll need to remove file:// from the beginning of currentPath

    Reading local files (via COM Automation)

    private static byte[] readBinaryFile(string fileName)
    {
        const int adTypeBinary = 1;
    
        using (dynamic adoCom = System.Runtime.InteropServices.Automation.AutomationFactory.CreateObject(@"ADODB.Stream"))
        {
            adoCom.Type = adTypeBinary;
            adoCom.Open();
            adoCom.LoadFromFile(fileName);
    
            return adoCom.Read();
        }
    }
    

    Writing local files (also via COM Automation)

    private static void writeBinaryFile(string fileName, byte[] binaryArray)
    {
        const int adTypeBinary = 1;
        const int adSaveCreateOverWrite = 2;
        using (dynamic adoCom = System.Runtime.InteropServices.Automation.AutomationFactory.CreateObject(@"ADODB.Stream"))
        {
            adoCom.Type = adTypeBinary;
            adoCom.Open();
            adoCom.Write(binaryArray);
            adoCom.SaveToFile(fileName, adSaveCreateOverWrite);
        }
    }
    

    You may also want to check Silverlight COM Toolkit. I don’t use it, though.

    Good luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Pretty much what the title says really. We have some code that is .NET
I have some code that is a result of pretty much my first effort
I have some php code that is pretty much being duplicated save for some
I have heard mention that some desktop applications are pretty much just wrappers for
I have a problem that annoys me pretty much. I'm grabbing some content from
I have a piece of code that pretty much reduces down to: template<class T>
This is pretty much the same problem i have, except with very different code:
I have built a PHP calendar system and have pretty much everything done but
We have a Struts 2 web application that's used by pretty much every employee
I have two domains with pretty much the same code on both, (version control)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.