kommander000
kommander000

Reputation: 3

C# - Adding emoticons into a WPF application

I am currently creating a side project that deals with dual-user communication using DuplexChanneling in C#.

My problem is the implementation of this very simple, yet tedious aspect--emotes.

Here is the code that is giving me trouble:

Hashtable emotions;

    void CreateEmotions()
    {
        emotions = new Hashtable(6);
        emotions.Add(":)", ChattingClient.Properties.Resources.regular_smile);
        emotions.Add(":(", ChattingClient.Properties.Resources.sad_smile);            
    }

    void AddEmotions()
    {
        foreach (string emote in emotions.Keys)
        {
            while (TextBoxDisplay1.Text.Contains(emote))
            {
                int ind = TextBoxDisplay1.Text.IndexOf(emote);
                TextBoxDisplay1.Select(ind, emote.Length);
                Clipboard.SetImage((Image)emotions[emote]);
                TextBoxDisplay1.Paste();
            }
        }
    }

The error is because Clipboard.SetImage((Image)emotions[emote]);

It states both:

Error 1 The best overloaded method match for 'System.Windows.Clipboard.SetImage(System.Windows.Media.Imaging.BitmapSource)' has some invalid arguments

and

Error 2 Argument 1: cannot convert from 'System.Windows.Controls.Image' to 'System.Windows.Media.Imaging.BitmapSource' C:\Users\StandardGuy\Desktop\ChattingApplication\ChattingClient\ChattingClient\MainWindow.xaml.cs 68 41 ChattingClient

Upvotes: 0

Views: 993

Answers (1)

Engioneer
Engioneer

Reputation: 76

  • Import the namespaces to use BitmapImage and Uri
  • Using the Solution Explorer go to the Resources folder of the project, then go to the properties of each image and set the Build Action to Resources
Hashtable emotions;

void CreateEmotions()
{
    emotions = new Hashtable(6);
    emotions.Add(":)", new BitmapImage(new Uri("/Resources/name_of_picture1.png", UriKind.Relative)));
    emotions.Add(":(", new BitmapImage(new Uri("/Resources/name_of_picture2.png", UriKind.Relative)));
}

void AddEmotions()
{
    foreach (string emote in emotions.Keys)
    {
        while (TextBoxDisplay1.Text.Contains(emote))
        {
            int ind = TextBoxDisplay1.Text.IndexOf(emote);
            TextBoxDisplay1.Select(ind, emote.Length);
            Clipboard.SetImage((BitmapImage)emotions[emote]);
            TextBoxDisplay1.Paste();
        }

    }

}

Hope this helps!

Upvotes: 1

Related Questions