out of memory Image.FromFile

In the Image.FromFile documentation, an OutOfMemoryException can be throw if: The file does not have a valid image format. -or- GDI+ does not support the pixel format of the file. Check your image format. Also, if you want to close the stream right after loading the image, you must make a copy of the image. … Read more

How to use jquery or ajax to update razor partial view in c#/asp.net for a MVC project

You’ll need AJAX if you want to update a part of your page without reloading the entire page. main cshtml view <div id=”refTable”> <!– partial view content will be inserted here –> </div> @Html.TextBox(“yearSelect3″, Convert.ToDateTime(tempItem3.Holiday_date).Year.ToString()); <button id=”pY”>PrevY</button> <script> $(document).ready(function() { $(“#pY”).on(“click”, function() { var val = $(‘#yearSelect3’).val(); $.ajax({ url: “/Holiday/Calendar”, type: “GET”, data: { year: … Read more

How to get DateTime from the internet?

For environments where port 13 is blocked, time from NIST can be web scraped as below, public static DateTime GetNistTime() { DateTime dateTime = DateTime.MinValue; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(“http://nist.time.gov/actualtime.cgi?lzbc=siqm9b”); request.Method = “GET”; request.Accept = “text/html, application/xhtml+xml, */*”; request.UserAgent = “Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)”; request.ContentType = “application/x-www-form-urlencoded”; request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore); //No … Read more

C# Create New T()

Take a look at new Constraint public class MyClass<T> where T : new() { protected T GetObject() { return new T(); } } T could be a class that does not have a default constructor: in this case new T() would be an invalid statement. The new() constraint says that T must have a default … Read more

Is it possible to call a C function from C#.Net

The example will be, for Linux: 1) Create a C file, libtest.c with this content: #include <stdio.h> void print(const char *message) { printf(“%s\\n”, message); } That’s a simple pseudo-wrapper for printf. But represents any C function in the library you want to call. If you have a C++ function don’t forget to put extern C … Read more

How do I get the current line number?

In .NET 4.5 / C# 5, you can get the compiler to do this work for you, by writing a utility method that uses the new caller attributes: using System.Runtime.CompilerServices; static void SomeMethodSomewhere() { ShowMessage(“Boo”); } … static void ShowMessage(string message, [CallerLineNumber] int lineNumber = 0, [CallerMemberName] string caller = null) { MessageBox.Show(message + ” … Read more

Parse Math Expression [duplicate]

I urge caution against choosing an existing generic expression evaluator over a purpose-built math evaluator. The reason for this is expression evaluators are not limited to math. A clever individual could use this to create an instance of any type in the framework and call any method on the type, and that would allow him … Read more