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 9181769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:23:56+00:00 2026-06-17T18:23:56+00:00

Let’s say I have a class, this class contains a public property which is

  • 0

Let’s say I have a class, this class contains a public property which is a System.Drawing.Bitmap, but I want the consumer of my class to be able to set this value with a number of different types of image, without really having to think about what they are passing in, I will do the conversions neccessary behind the scenes. This is what I mean:

var myBitmapImage = new BitmapImage();
var writeableBitmap = new WriteableBitmap(myBitmapImage);
var mySystemDrawingBitmap = new Bitmap(@"A:\b.c");

var classOne = new TestClass();
var classTwo = new TestClass();
var classThree = new TestClass();

//This should work:
classOne.MyImage = myBitmapImage;

//This should also work:
classTwo.MyImage = writeableBitmap;

//This should work too
classThree.MyImage = mySystemDrawingBitmap;

At the moment I am thinking of something like this:

public class TestClass
{
    private Bitmap _myImage;

    public object MyImage
    {
        get
        {
            return _myImage;
        }

        set
        {
            if (value is Bitmap)
            {
                _myImage = (Bitmap)value;
            }

            if (value is BitmapImage)
            {
                var imageAsSystemDrawingBitmap = ConvertBitmapImageToBitmap((BitmapImage)value);
                _myImage = imageAsSystemDrawingBitmap;
            }

            if (value is WriteableBitmap)
            {
                var imageAsSystemDrawingBitmap = ConvertWriteableBitmapToBitmap((WriteableBitmap)value);
                _myImage = imageAsSystemDrawingBitmap;
            }

            throw new Exception("Invalid image type");
        }
    }

    private Bitmap ConvertWriteableBitmapToBitmap(WriteableBitmap value)
    {
        //do work here
        return null;
    }

    private Bitmap ConvertBitmapImageToBitmap(BitmapImage value)
    {
        //do work here
        return null;
    }
}

But using an object and casting feels very 2001, and I am sure there must be a more eloquent way to achieve this. Is there, or is this a bad idea in the first place?

  • 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-06-17T18:23:57+00:00Added an answer on June 17, 2026 at 6:23 pm

    You could create a BitmapFactory class that behaves as a factory to create a Bitmap, you can read more about the Factory Design Pattern:

    public class TestClass
    {
        public BitmapFactory BitmapFactory { get; set; }
        public Bitmap Bitmap { get { return this.BitmapFactory.Bitmap; } }
    }
    
    public interface IBitmapFactory
    {
        Bitmap Bitmap { get; }
    }
    
    public class BitmapFactory : IBitmapFactory
    {
        public Bitmap Bitmap { get; private set; }
    
        public BitmapFactory(Bitmap value)
        {
            this.Bitmap = value;
        }
    
        public BitmapFactory(BitmapImage value)
        {
            this.Bitmap = ConvertBitmapImageToBitmap(value);
        }
    
        public BitmapFactory(WriteableBitmap value)
        {
            this.Bitmap = ConvertWriteableBitmapToBitmap(value);
        }
    
        private Bitmap ConvertWriteableBitmapToBitmap(WriteableBitmap value)
        {
            //do work here
            return null;
        }
    
        private Bitmap ConvertBitmapImageToBitmap(BitmapImage value)
        {
            //do work here
            return null;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say that I have classes like this: public class Parent { public int
Let's say I don't have photoshop, but I want to make pattern files (.pat)
Let's say I have the following entity: public class Store { public List<Product> Products
Let's say I have a main folder in my website named test which contains
Let's say I have one class User, and it has a property of type
Let's say I have a Person class with FirstName and LastName . I want
Let say you have an XML like this: <?xml version=1.0 encoding=utf-8?> <Class HashCode=307960707> <Person>
Let's say we have this simple class: class example { bool m_isCanceled; example() :
Let's say I have a method in java, which looks up a user in
Let me explain best with an example. Say you have node class that can

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.