


Detailed introduction to the sample code of the second version of the Data Url generation tool C#
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
Picture Encoding judgment problemBefore, 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; }
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;
//创建线程来生成DataUrl System.Threading.Thread thd = new System.Threading.Thread(new ParameterizedThreadStart(buildDataUrl)); thd.Start(textBox_saveDir.Text);
/// <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)); } }
//计算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));
/// <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); }
/// <summary> /// 清空文本框 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button_clear_Click(object sender, EventArgs e) { textBox1.Clear(); fullDataUrl = string.Empty; }
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

Guide to C# Serialization. Here we discuss the introduction, steps of C# serialization object, working, and example respectively.

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.
