Details the sample code for the second edition of the Data URL Generation Tool C # version

Source: Internet
Author: User





Why is there a second edition






First of all, thank Jenlynn students of the message "DATA URL two generation method, C # and HTML5 the same generated base64 code looks different, how can we get them to agree?"



Second, bugs and anomalies were found in the study of the problem.
Bug: Image encoding judgment problem, no matter what extension is the default use of PNG encoding.
Exception: ContextSwitchDeadlock detected



Interface Preview






An improved method for related problems



Image coding and judging problem



Before the main is to forget to get the extension is preceded by a dot.
Related code:

string ext = Path.GetExtension (path) .ToLower ();
                // Determine the encoding format used according to the file extension
                // Note that the extension is dotted!
                switch (ext)
                {
                    case ".gif":
                        fmt = System.Drawing.Imaging.ImageFormat.Gif;
                        break;
                    case ".jpg":
                    case ".jpeg":
                        fmt = System.Drawing.Imaging.ImageFormat.Jpeg;
                        break;
                    case ".ico":
                        fmt = System.Drawing.Imaging.ImageFormat.Icon;
                        break;
                    default:
                        ext = "png";
                        break;
                }
ContextSwitchDeadlock detected
Solution description
StackOverflow mentioned the use of BackgroundWorker, I use threads here; but after testing found: due to performance problems when TextBox displays large text, when the thread interacts with TextBox, if the user does not operate, the window will not die; once there is any operation, the window is Not responding!
So you can only change the solution, use a compromise method, do not let the TextBox display all DataUrl strings, only display a part of them; use a variable "" to save the complete DataUrl string, and copy it to when you click the copy button Windows clipboard.
Related code

        /// <summary>
        /// Used to save the complete DataUrl
        /// </ summary>
        private string fullDataUrl = string.Empty;
Use threads

                // Create thread to generate DataUrl
                System.Threading.Thread thd = new System.Threading.Thread (new ParameterizedThreadStart (buildDataUrl));
                thd.Start (textBox_saveDir.Text);
Use delegation

        /// <summary>
        /// The TextBox delegate is used to achieve thread safety for accessing forms, components, etc. in a thread
        /// </ summary>
        /// <param name = "msg"> </ param>
        public delegate void textbox_delegate (string msg); /// <summary>
        /// The TextBox delegate implementation is used to achieve thread safety for accessing forms and components in threads
        /// </ summary>
        /// <param name = "msg"> </ param>
        public void textboxset (string msg)
        {if (textBox1 == null) return;
        if (textBox1.InvokeRequired)
            {
                textbox_delegate dt = new textbox_delegate (textboxset);
                textBox1.Invoke (dt, new object [] {msg});
            }
            else
            {
            int strLen = msg.Length;
            int step = 100;
            while (strLen> step)
                {
                    textBox1.AppendText (msg.Substring (msg.Length-strLen, step));
                    strLen-= step;
                }
                textBox1.AppendText (msg.Substring (msg.Length-strLen, strLen));
            }
        }
Optimized Base64 encoding

                // Calculate how many characters can be omitted after the Base64 encoded string
                int strLen = str.Length;
                string dyzf = str.Substring (strLen-1, 1);
                while ((dyzf == "A" || dyzf == "=") && strLen> 0)
                {
                    strLen-= 1;
                    dyzf = str.Substring (strLen-1, 1);
                } // Combine the complete Data Url
                fullDataUrl = "<img src = \" data: image / "+ ext +"; base64, "
                    + str.Substring (0, strLen)
                    + "\" width = \ "" + img.Width + "\" height = \ "" + img.Height + "\" /> ";
                    // The TextBox is defined to display only 20,000 characters at most, and the extra cut will not be displayed, otherwise the performance is too poor.
                int showLen = 20000; if (showLen> fullDataUrl.Length)
                {
                    showLen = fullDataUrl.Length;
                }
                textboxset (fullDataUrl.Substring (0, showLen));
        /// <summary>
        /// Copy the complete Data Url to the Windows clipboard.
        /// </ summary>
        /// <param name = "sender"> </ param>
        /// <param name = "e"> </ param>
        private void button_copy_Click (object sender, EventArgs e)
        {
            Clipboard.SetText (fullDataUrl);
        }
        /// <summary>
        /// Clear the text box
        /// </ summary>
        /// <param name = "sender"> </ param>
        /// <param name = "e"> </ param>
        private void button_clear_Click (object sender, EventArgs e)
        {
            textBox1.Clear ();
            fullDataUrl = string.Empty;
        }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.