Home > Backend Development > C#.Net Tutorial > Detailed introduction to the sample code of the second version of the Data Url generation tool C#

Detailed introduction to the sample code of the second version of the Data Url generation tool C#

黄舟
Release: 2017-03-11 13:48:16
Original
1925 people have browsed it

Why is there a second edition?


 First of all, thank you to jenlynn for her message: "There are two ways to generate DATA URL, C# and HTML5. Both are the same." The generated base64 encoding seems to be different, is there any way to make them agree?"

Secondly, bugs and anomalies were discovered while studying this issue.
Bug: Image encoding judgment problem, no matter what extension, PNG encoding is used by default.
Exception: ContextSwitchDeadlock detected

Interface preview


Detailed introduction to the sample code of the second version of the Data Url generation tool C#

##Improvement methods for related issues


Picture Encoding judgment problem

Before, it was mainly because I forgot that the obtained extension was preceded by a dot.

Related code:

string ext = Path.GetExtension(path).ToLower();
                //根据文件的扩展名确定使用的编码格式
                //注意扩展名是带点的!
                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;
                }
Copy after login

ContextSwitchDeadlock detected

Solution Method description StackOverflow mentioned using BackgroundWorker, I use threads here; however, after testing, it was found that due to performance issues when TextBox displays large text, when threads interact with TextBox, if the user does not operate, the window will not die. ; Once any operation is performed, the window will not respond!
So we can only change the solution, use a compromise method, do not let the TextBox display the entire DataUrl string, only display part of it; use a variable "" to save the complete DataUrl string, and click the copy button to Copy it to the Windows clipboard.

Related code

        /// <summary>
        /// 用于保存完整的DataUrl
        /// </summary>
        private string fullDataUrl = string.Empty;
Copy after login

Use threads

                //创建线程来生成DataUrl
                System.Threading.Thread thd = new System.Threading.Thread(new ParameterizedThreadStart(buildDataUrl));
                thd.Start(textBox_saveDir.Text);
Copy after login

Use delegates

        /// <summary>
        /// TextBox委托,用于实现线程中访问窗体、组件等的线程安全性
        /// </summary>
        /// <param name="msg"></param>
        public delegate void textbox_delegate(string msg);        /// <summary>
        /// TextBox委托实现,用于实现线程中访问窗体、组件等的线程安全性
        /// </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));
            }
        }
Copy after login

Optimize Base64 encoding

                //计算Base64编码的字符串后部分有多少可以省略的字符
                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);
                }                //组合完整的Data Url
                fullDataUrl = "<img src=\"data:image/" + ext + ";base64,"
                    + str.Substring(0, strLen)
                    + "\" width=\"" + img.Width + "\" height=\"" + img.Height + "\" />";                
                    //这里定义TextBox最多只显示20000个字符,多余的裁掉不显示了,不然性能太差。
                int showLen = 20000;                if (showLen > fullDataUrl.Length)
                {
                    showLen = fullDataUrl.Length;
                }
                textboxset(fullDataUrl.Substring(0, showLen));
Copy after login
        /// <summary>
        /// 将完整的Data Url复制到Windows剪贴板中。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_copy_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(fullDataUrl);
        }
Copy after login
        /// <summary>
        /// 清空文本框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_clear_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
            fullDataUrl = string.Empty;
        }
Copy after login

The above is the detailed content of Detailed introduction to the sample code of the second version of the Data Url generation tool C#. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template