Skip to main content

Posts

Showing posts with the label C#

Using C# to run WSL process (python script) on Windows

Every times when Nvidia updates its drivers, the task with Tensorflow is more or least impacted. Quote from TF's website: TensorFlow 2.10 was the last TensorFlow release that supported GPU on native-Windows. Starting with TensorFlow 2.11, you will need to install TensorFlow in WSL2, or install tensorflow or tensorflow-cpu and, optionally, try the TensorFlow-DirectML-Plugin. PyTorch works fine with CUDA. With some modifications on environment variables to use the toolkit 12.1, it is easy to setup for running PyTorch projects on Windows with GPU. To attempt to run TF project on WSL2 with GPU, there comes an idea to create a C# project to run a WSL-python-script process on Windows. Create a C# WinForm project with Visual Studio. Set the project with x64 platform (replace with "Any CPU")! (Otherwise, the running process will be in 32-bit, on which, WSL is not installed.) The code looks like this ( Please refer to the original code ): using (var proc...

Tool to Convert Source Code to HTML Encoded Text in C#

It will be convenient to have a tool converting the source code to HTML encoded text, before I can post it on my blog article. The following C# code is my tool to do the job: public FormMain() { InitializeComponent(); } private void buttonExit_Click(object sender, EventArgs e) { Close(); } private void buttonRun_Click(object sender, EventArgs e) { string src = textBoxSrc.Text; string html = System.Net.WebUtility.HtmlEncode(src); textBoxHTML.Text = html; textBoxHTML.SelectAll(); textBoxHTML.Copy(); } private void textBoxSrc_MouseClick(object sender, MouseEventArgs e) { textBoxSrc.Text = ""; } The UI: Usage: Click on the textBoxSrc, the text in textBoxSrc will be cleaned. (line 26) Copy the source code to the textBoxSrc. Click the buttonRun. The converted text will be displayed on the textBoxHTML, in the same time, the text will also be copied to the clipboard for further use. (line 13 ~ 21) You may use Visual Studio Community to create the p...

Flicker free Drawing with WinForm in C#

Just create a C# WinForm Project, Name the form "FormMain" and add a button "buttonRun". Use the code below then you can test it. Main idea is: Draw some image on a bitmap first, then "paste" the bitmap on the graphics of the form. This will reduce the "flicking" of the window. Code: ================================== public partial class FormMain : Form { bool isDrawing = false; Bitmap gBmp = null; Graphics gGraphImg = null; // this variables are just for the Curve int gX = 350, gY = 200; int offX = 180, offY = 100; public FormMain() { InitializeComponent(); CreateBitmapAndGraphic(); } // gBmp & gGraph will be created, when the form is initialized or resized. private void CreateBitmapAndGraphic() { if (gBmp != null) { gBmp.Dispose(); // Dispose it to prevent memory usage too high, when redraw too many times. } gBmp =...