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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:58:12+00:00 2026-06-17T15:58:12+00:00

I’m creating a class to help modify images in a GUI. The images can

  • 0

I’m creating a class to help modify images in a GUI. The images can be in either a PictureBox or a DataGridViewImageCell. The class will store as a list all images that can be modified. Each instance of the class will use either PictureBox or DataGridViewImageCells but not both.

Is there a way to handle both inputs? As in, is there some common interface between them, or a way to cast them to some common parent class?

  • 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-17T15:58:12+00:00Added an answer on June 17, 2026 at 3:58 pm

    Well, you can always treat them as object, but I would suggest that your class simply provide two overloads for those two types specifically:

    class MyClass
    {
        public void AddImage(PictureBox image)
        {
    
        }
    
        public void AddImage(DataGridViewImageCell image)
        {
    
        }
    }
    

    How you store that internally is up to you. Perhaps you have a List<object> that you cast into/out-of, or perhaps you maintain two separate lists, one for each type, or perhaps you simply add each image to the containing object’s children collection.

    As for a shared class, I believe from the the MSDN that they do not share a base class other than Object so you’ll have to stick with that or wrap them with your own containing class.

    EDIT: Here’s a more fully fleshed out way handling it with wrapping interfaces and factories.

    First, define an interface with a shared based type (in this case object) with which you can pass around the images:

    public interface IImageWrapper
    {
        object RawImage { get; }
    }
    
    public class PictureBoxImageWrapper : IImageWrapper
    {
        public object RawImage { get; private set; }
    
        public PictureBoxMyImage(PictureBox image)
        {
            this.RawImage = image;
        }
    }
    
    public class DataGridViewImageCellImageWrapper : IImageWrapper
    {
        public object RawImage { get; private set; }
    
        public DataGridViewImageCellImageWrapper(DataGridViewImageCell image)
        {
            this.RawImage = image;
        }
    }
    

    Then a simple factory for the user API to wrap the images with and have compile-time safety as to which image types are strictly supported:

    public class ImageWrapperFactory
    {
        public IImageWrapper Create(PictureBox image)
        {
            return new PictureBoxImageWrapper(image);
        }
    
        public IImageWrapper Create(DataGridViewImageCell image)
        {
            return new DataGridViewImageCellImageWrapper(image);
        }
    }
    

    Then your displaying class or view can receive these IImageWrapper objects and convert them into usable types:

    public class MyDisplayClass
    {
        private List<IImageWrapper> Images = new List<IImageWrapper>();
    
        public void AddImage(IImageWrapper image)
        {
            Images.Add(image);
        }
    
        private void AddImageToContainer(IImageWrapper image)
        {
            object rawImage = image.RawImage;
    
            if (rawImage is PictureBox)
                AddImageToContainerImpl((PictureBox)rawImage);
            else if (rawImage is DataGridViewImageCell)
                AddImageToContainerImpl((DataGridViewImageCell)rawImage);
            else
                throw new NotSupportedException();
        }
    
        private void AddImageToContainerImpl(PictureBox image)
        {
            //add to container
        }
    
        private void AddImageToContainerImpl(DataGridViewImageCell image)
        {
            //add to container
        }
    }
    

    Some sample usage:

    PictureBox myImage = ...
    MyDisplayClass myView = ...
    
    myView.AddImage(ImageWrapperFactory.Create(myImage));
    

    As this is, the underlying type of the image isn’t too strongly enforced. If you wish, you can update the factory to return strongly typed IImageWrapper objects as PictureBoxImageWrapper and in turn have the display class strongly typed against those that it supports, but that kinda defeats the purpose (you may as well strongly type against PictureBox/DataGridViewImageCell as in my first answer.

    EDIT: Yet another way is to leverage implicit operators for your “factory”. This way your API just “passes” in the raw image objects and if they’re supported, an implicit operator will be found. If they’re not, then you’ll get a compile time exception:

    public sealed class ImageWrapper
    {
        public object RawData { get; private set; }
    
        private ImageWrapper(object rawData)
        {
            this.RawData = rawData;
        }
    
        public static implicit operator ImageWrapper(PictureBox image)
        {
            return new ImageWrapper(image);
        }
    
        public static implicit operator ImageWrapper(DataGridViewImageCell image)
        {
            return new ImageWrapper(image);
        }
    }
    
    
    PictureBox myImage = ...
    ImageControl unsupportedImage = ... 
    MyDisplayClass myView = ...
    
    myView.AddImage(myImage); //OK!
    myView.AddImage(unsupportedImage);// compile error!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am using JSon response to parse title,date content and thumbnail images and place
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Does anyone know how can I replace this 2 symbol below from the string
I need a function that will clean a strings' special characters. I do NOT

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.