


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; }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;Use threads
//创建线程来生成DataUrl System.Threading.Thread thd = new System.Threading.Thread(new ParameterizedThreadStart(buildDataUrl)); thd.Start(textBox_saveDir.Text);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)); } }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));
/// <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!

C# is widely used in enterprise-level applications, game development, mobile applications and web development. 1) In enterprise-level applications, C# is often used for ASP.NETCore to develop WebAPI. 2) In game development, C# is combined with the Unity engine to realize role control and other functions. 3) C# supports polymorphism and asynchronous programming to improve code flexibility and application performance.

C# and .NET are suitable for web, desktop and mobile development. 1) In web development, ASP.NETCore supports cross-platform development. 2) Desktop development uses WPF and WinForms, which are suitable for different needs. 3) Mobile development realizes cross-platform applications through Xamarin.

The C#.NET ecosystem provides rich frameworks and libraries to help developers build applications efficiently. 1.ASP.NETCore is used to build high-performance web applications, 2.EntityFrameworkCore is used for database operations. By understanding the use and best practices of these tools, developers can improve the quality and performance of their applications.

How to deploy a C# .NET app to Azure or AWS? The answer is to use AzureAppService and AWSElasticBeanstalk. 1. On Azure, automate deployment using AzureAppService and AzurePipelines. 2. On AWS, use Amazon ElasticBeanstalk and AWSLambda to implement deployment and serverless compute.

The combination of C# and .NET provides developers with a powerful programming environment. 1) C# supports polymorphism and asynchronous programming, 2) .NET provides cross-platform capabilities and concurrent processing mechanisms, which makes them widely used in desktop, web and mobile application development.

.NETFramework is a software framework, and C# is a programming language. 1..NETFramework provides libraries and services, supporting desktop, web and mobile application development. 2.C# is designed for .NETFramework and supports modern programming functions. 3..NETFramework manages code execution through CLR, and the C# code is compiled into IL and runs by CLR. 4. Use .NETFramework to quickly develop applications, and C# provides advanced functions such as LINQ. 5. Common errors include type conversion and asynchronous programming deadlocks. VisualStudio tools are required for debugging.

C# is a modern, object-oriented programming language developed by Microsoft, and .NET is a development framework provided by Microsoft. C# combines the performance of C and the simplicity of Java, and is suitable for building various applications. The .NET framework supports multiple languages, provides garbage collection mechanisms, and simplifies memory management.

C# and .NET runtime work closely together to empower developers to efficient, powerful and cross-platform development capabilities. 1) C# is a type-safe and object-oriented programming language designed to integrate seamlessly with the .NET framework. 2) The .NET runtime manages the execution of C# code, provides garbage collection, type safety and other services, and ensures efficient and cross-platform operation.


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

SublimeText3 English version
Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools

Dreamweaver CS6
Visual web development tools
