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

  • Home
  • SEARCH
  • 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 7501955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:42:18+00:00 2026-05-29T20:42:18+00:00

Possible Duplicate: Smooth text animation (Marquee) using WPF I am just wondering if there

  • 0

Possible Duplicate:
Smooth text animation (Marquee) using WPF

I am just wondering if there is some best coding approach to scroll text and images horizontally in WPF?

Basically I used some code to do it like

        private void PopulateCanvas()
                {

                    canvas1.Width = RootGrid.ActualWidth;
                    canvas1.Height = RootGrid.ActualHeight;

                    foreach (var item in PluginContentItems)
                    {
                        if (IsImage(item.IPluginContentItemElements))
                        {
                            string imageName = GetImageName(item.IPluginContentItemElements);

                            if (IsGifImage(item.IPluginContentItemElements))
                                AddGIFImage(imageName);
                            else
                                AddImage(imageName);
                        }
                        else // Text
                        {
                            string text = GetText(item.IPluginContentItemElements);
                            SolidColorBrush brush = GetFontColor(item.IPluginContentItemElements);
                            AddTextBlock(text, brush);
                        }
                    }

                    canvas1.Dispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(delegate(Object state)
                    {
                        var node = textBlocks.First;

                        while (node != null)
                        {
                            if (node.Previous != null)
                            {
                                if (node.Previous.Value.GetType().FullName.Contains("TextBlock"))
                                    Canvas.SetLeft(node.Value, Canvas.GetLeft((TextBlock)node.Previous.Value) + ((TextBlock)node.Previous.Value).ActualWidth); // + gap
                                else if (node.Previous.Value.GetType().FullName.Contains("GifImage"))
                                    Canvas.SetLeft(node.Value, Canvas.GetLeft((GifImage)node.Previous.Value) + ((GifImage)node.Previous.Value).ActualWidth); // + gap
                                else if (node.Previous.Value.GetType().FullName.Contains("Controls.Image"))
                                    Canvas.SetLeft(node.Value, Canvas.GetLeft((Image)node.Previous.Value) + ((Image)node.Previous.Value).ActualWidth); // + gap
                            }
                            else
                            {
                                if (node.Value.GetType().FullName.Contains("TextBlock"))
                                    Canvas.SetLeft((TextBlock)node.Value, canvas1.Width); // + gap
                                if (node.Value.GetType().FullName.Contains("Image"))
                                    Canvas.SetLeft((Image)node.Value, canvas1.Width); // + gap
                                if (node.Value.GetType().FullName.Contains("GifImage"))
                                    Canvas.SetLeft((GifImage)node.Value, canvas1.Width); // + gap
                            }

                            node = node.Next;
                        }

                        return null;

                    }), null);

                }


     void timer_Elapsed(object sender, ElapsedEventArgs e)
            {
                canvas1.Dispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(delegate(Object state)
                {
                    var node = textBlocks.First;
                    var lastNode = textBlocks.Last;

                    while (node != null)
                    {
                        double newLeft = Canvas.GetLeft(node.Value) - move_amount;

                        double w1 = 0;

                        if (node.Value is TextBlock)
                            w1 = ((TextBlock)node.Value).ActualWidth;

                        if (node.Value is Image)
                            w1 = ((Image)node.Value).ActualWidth;

                        if (node.Value is GifImage)
                            w1 = ((GifImage)node.Value).ActualWidth;

                        if (newLeft < (0 - w1)) // + gap
                        {
                            textBlocks.Remove(node);

                            var lastNodeLeftPos = Canvas.GetLeft(lastNode.Value);

                            textBlocks.AddLast(node);

                            if (lastNode.Value.GetType().FullName.Contains("TextBlock"))
                            {
                                // + gap
                                if ((lastNodeLeftPos + ((TextBlock)lastNode.Value).ActualWidth) > canvas1.Width) // Last element is offscreen
                                    newLeft = lastNodeLeftPos + ((TextBlock)lastNode.Value).ActualWidth; // + gap
                                else
                                    newLeft = canvas1.Width; //  +gap;
                            }
                            else if (lastNode.Value.GetType().FullName.Contains("GifImage"))
                            { // + gap
                                if ((lastNodeLeftPos + ((GifImage)lastNode.Value).ActualWidth) > canvas1.Width) // Last element is offscreen
                                    newLeft = lastNodeLeftPos + ((GifImage)lastNode.Value).ActualWidth; // + gap
                                else
                                    newLeft = canvas1.Width; // + gap
                            }
                            else if (lastNode.Value.GetType().FullName.Contains("Controls.Image"))
                            { // + gap
                                if ((lastNodeLeftPos + ((Image)lastNode.Value).ActualWidth) > canvas1.Width) // Last element is offscreen
                                    newLeft = lastNodeLeftPos + ((Image)lastNode.Value).ActualWidth; // + gap
                                else
                                    newLeft = canvas1.Width; // + gap
                            }
                        }

                        Canvas.SetLeft(node.Value, newLeft);

                        node = node == lastNode ? null : node.Next;
                    }

                    return null;

                }), null);
            }


private void AddGIFImage(string file)
        {
            try
            {
                string pathToImage = System.IO.Path.Combine(Settings.ContentFolderPath, file);

                Uri u = new Uri(pathToImage);
                GifImage gif = new GifImage(u);
                gif.Height = canvas1.Height; //  canvas1.Height / koeffImage;
                canvas1.Children.Add(gif);
                Canvas.SetTop(gif, 0);
                Canvas.SetLeft(gif, -999);
                textBlocks.AddLast(gif);
            }
            catch (Exception ex)
            {
                HasError = true;
                ErrorMessage = ex.Message;
            }
        }

        private void AddImage(string file)
        {
            try
            {
                string pathToImage = System.IO.Path.Combine(Settings.ContentFolderPath, file);

                Image image = new Image();
                BitmapImage src = new BitmapImage();
                src.BeginInit();
                src.UriSource = new Uri(pathToImage, UriKind.Absolute);
                src.EndInit();
                double ratio = src.Width / src.Height;
                image.Source = src;
                image.Stretch = Stretch.Uniform;
                image.Height = canvas1.Height;
                image.Width = canvas1.Height * ratio;
                canvas1.Children.Add(image);
                Canvas.SetTop(image, 0);
                Canvas.SetLeft(image, -999);
                textBlocks.AddLast(image);
            }
            catch (Exception ex)
            {
                HasError = true;
                ErrorMessage = ex.Message;
            }
        }

        void AddTextBlock(string Text, SolidColorBrush color)
        {
            try
            {
                double w = MeasureTextSize(Text, fontFamily, fontStyle, fontWeight, fontStretch, fontSize).Width;

                TextBlock tb = new TextBlock();
                tb.Text = Text;
                tb.FontSize = 28;
                tb.FontWeight = FontWeights.Normal;
                tb.Foreground = color;
                // tb.Background = Brushes.Blue;
                tb.FontSize = fontSize = canvas1.Height / koeff;
                tb.Width = w;

                canvas1.Children.Add(tb);

                Canvas.SetTop(tb, 0);
                Canvas.SetLeft(tb, -9999);

                textBlocks.AddLast(tb);
            }
            catch (Exception ex)
            {
                HasError = true;
                ErrorMessage = ex.Message;
            }
        }
  • 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-29T20:42:20+00:00Added an answer on May 29, 2026 at 8:42 pm

    As a user specified in the comments, you should do this in XAML. It’s easier, cleaner and yes, it is a best practice. You can use a ListBox of Items and just play with it’s ItemsPanel data template. I also recommend further learning on Storyboards.

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

Sidebar

Related Questions

Possible Duplicate: .NET - What’s the best way to implement a catch all exceptions
Possible Duplicate: How do you send email from a Java app using Gmail? How
Possible Duplicate: Parsing text in C Say I have written to a text file
Possible Duplicate: how to calculate difference between two dates using java I'm trying something
Possible Duplicate: regex for URL including query string I have a text or message.
Possible Duplicate: Android How to draw a smooth line following your finger I'm new
Possible Duplicate: iPhone smooth sketch drawing algorithm I'm looking for an actual demonstrable example
Possible Duplicate: Why do I see a double variable initialized to some value like
Possible Duplicate: Create Generic method constraining T to an Enum Is there any reason
Possible Duplicate: Best way to really grok Java for a C# guy I worked

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.