Imaging on C#

There are many possibilities to process graphical information in .Net.image
Microsoft has Graphics Device Interface for imaging. Its functionality is provided by System.Drawing.Common namespace in .Net Core and Framework.
By means of C# imaging it is possible:

  • to create images
  • to edit images
  • to create 2D animations
  • to create math graphics
  • to draw formatted text

Intro of imaging on C#

Adding imaging namespaces

First of all, System.Drawing.Common should be installed. It is downloadable from nuget.

using System.Drawing;
using System.Drawing.Imaging;
Some classes
  • Image – an abstract class that provides basic functionality of GDI
  • Bitmap – inherits Image, represents pixel image
  • Graphics – inherits Image, represent drawing surface
Load image from file and save to file
...
   Image image = Image.FromFile("path");  //constructor with string parameter (path to file)

   image.Save("otherpath");  // saving image
...
Codec
mime typeextension
image/apng.apng
image/bmp.bmp
image/jpeg.jpg, .jpeg
image/png.png
image/webp.webp

System.Drawing.Common only works with Bitmap images. When image is opened GDI uses decoder to transform it to Bitmap. When image is saved GDI uses encoder to prepare image to be saved with a needed extension. Encoder and decoder are called Codec. If Codec is not specified the default one will be used.

Method for getting encoder

...
   public static ImageCodecInfo GetEncoder(string mimetype)
   {
      return ImageCodecInfo.GetImageEncoders().FirstOrDefault(e => e.MimeType == mimetype);
   }
...

Method for getting decoder

...
   public static ImageCodecInfo GetDecoder(string mimetype)
   {
      return ImageCodecInfo.GetImageDecoders().FirstOrDefault(e => e.MimeType == mimetype);
   }
...

How to cut picture from image on C#

Method for cutting Rectangle (at point (x,y), with height h and width w) from Image.

...
   public static Image Cut(int x, int y, int w, int h, Image image)
   {
      Rectangle rect = new Rectangle(x, y, w, h);

      using Bitmap target = new Bitmap(w, h);

      using (Graphics g = Graphics.FromImage(target))
      {
         g.DrawImage(image, 0, 0, rect, GraphicsUnit.Pixel);
      }

      image = new Bitmap(target, new Size(target.Width, target.Height));

      return image;
   }
...

How to save image with a specific number of bytes

Method for changing image in order to make it consist of needed number of bytes.
It reduces image quality until its size is less than parameter value (memoryInKb) and then saves it to provided path.

...
   public static void Save(string fullpath, int memoryInKb, Image image)
   {
      long qual = 101;

      ImageCodecInfo imagecodecinfo = GetEncoder("image/jpeg");  //GetEncoder is set earlier

      System.Drawing.Imaging.Encoder encoder = System.Drawing.Imaging.Encoder.Quality;

      using EncoderParameters encoderparams = new EncoderParameters(1);
      double current_size=0;
      do
      {
         qual--;
         EncoderParameter encoderparam = new EncoderParameter(encoder, qual);
         encoderparams.Param[0] = encoderparam;
         
         using MemoryStream mem = new MemoryStream();
         image.Save(mem, imagecodecinfo, encoderparams);
         string text = Convert.ToBase64String(mem.ToArray());
         current_size = Encoding.Default.GetByteCount(text) / 1.333 / 1024;
      }
      while (current_size>memoryInKb);

      image.Save(fullpath, imagecodecinfo, encoderparams);
   }
...

How to change image extension on c#

...
   public static void ChangeExtension(string originpath,string destinationpath, ImageFormat format)
   {
     using Image image = Image.FromFile(originpath);

     image.Save(destinationpath, format);
   }
...

How to resize image on c#

Method that returns resized image with a spesific size.

...
   public static Image Resize(Size size, Image image)
   {
     return new Bitmap(image, size);
   }
...

How to scale image on c#

Method that proportionally resizes image. It uses the previous method Resize.

...
   public static Image Scale(bool relation, int size, Image image)
   {
      double A = image.Width, B = image.Height;
      if (relation)
      {
         image = Resize(new Size(size, (int)(size * B / A)), image);
      }
      else
      {
         image = Resize(new Size((int)(size / B * A), size), image);
      }
      return image;
   }
...

How to draw rectangle over image using c#

...
   public static Image DrawRect(Image image, int x, int y, int w,int h)
   {
      using Pen pen = new Pen(Color.Black, 1);
      Bitmap bitmap = new Bitmap(image);
      Graphics graphics = Graphics.FromImage(bitmap);
      graphics.DrawRectangle(pen, x, y, w, h);
      return bitmap;
   }
...

Sitemap generator application

You can download source from our repo or download portable program.

What is Sitemap?

sitemap
Sitemap is a set of urls to website pages. Site owner should put the most important page urls with some extra information like priority number, modification date and changes frequency to sitemap. As stated in sitemap protocol description:

Using the Sitemap protocol does not guarantee that web pages are included in search engines, but provides hints for web crawlers to do a better job of crawling your site.

How to create sitemap?

There are some options of making sitemaps:

  • Include urls to an xml document manually
  • Include urls to an xml document recursively by means of web client and html parser
  • use a plugin or create an endpoint that gives back an xml document (realization is different on each web platform)

Afterall, it is necessary to approve sitemap in search console. Otherwise search engines won’t register it.

.Net Core C# Application for generation of sitemap xml document

By means of this application it is possible to create sitemap for a website with unlimited number of pages.

The application crawls website in the order that links (a tags) are displayed on each page. What means that if a specific link is absent at all crawled pages this specific link won’t be added to sitemap.

Structure of sitemap generator

Full source code is kept in our repo. Here is a simplified explanation.
There are two main functional parts:

Crawl managing
  1. go to homepage and get links from its html
  2. save visited link, save found inner links (excluding visited)
  3. go to every inner link and get links from its html
  4. save visited link, save found inner links (excluding visited)
  5. keep cycle until the exclution of inner links and saved links is empty
...
   List new_urls = new List();
   List visited = new List();
...
   new_urls.Add(BaseUrl);  //first url
   do
   {
      List hrefs=new List();
      foreach (var url in new_urls)
      {
         string text =await _loader.Get(url);
         if (string.IsNullOrEmpty(text)) continue;

         visited.Add(url);
         List meta=Parser.GetAHrefs(text).Distinct().ToList();  //getting list of links
         Parser.Normalize(Domain,url,ref meta);
         if (Exclude)  //option to exclude query from url
             meta = meta.Select(u => u.Contains('?') ? u.Split('?')[0] : u).ToList();
         hrefs.AddRange(meta);
         hrefs = hrefs.Distinct().ToList();
       }
       new_urls = hrefs.Except(visited).ToList();   //excluding visited pages
    }
    while (new_urls.Count != 0);
...
HTML parsing

HtmlAgilityPack is used for html parsing.

...
   public static IEnumerable GetAHrefs(string text)
   {
      HtmlDocument document = new HtmlDocument();
      document.LoadHtml(text);
      var tags=document.DocumentNode.SelectNodes(".//*");
      foreach(var tag in tags)
      {
         if (tag.Name == "a" )
         {
            string href = tag.GetAttributeValue("href", string.Empty);
            if (!string.IsNullOrEmpty(href))
                yield return href;
         }
      }
   }
...
Application Interface

sitemapgenerator

  • Homepage url of a site
  • Domain name of a site
  • Include optional parameters to sitemap or not
  • Clear urls from query parameters

FAQ about HTTP Client with examples

What is HTTP Client?

In client-server model Server is the one who handle requests and send responses, Client is the one who send requests and handle responses.
client-server
Generally speaking, Client is a program, library or a program method that:

  1. Sends request with some data to the server
  2. Recieves response messege from server

That list is a minimal requirements to be considered a client.

Client can be both a high-level many functional library and Two request-response method code.

Wisely chosen Client is crucial for the performance of the web application that uses external API or deals with a large number of requests.
As a rule each language platform has its native tool to do client’s job.

Why do you need HTTP Client?

Nowadays, there are some standarts of biulding HTTP request messeges in client-server model. That is how various web systems can communicate. As you can see in RFC documentation, there are a lot of information about what is HTTP and how to use it. Usually a common developer doesn’t have time to look through it. But he doesn’t really have to.

Client is developed to match standarts of a protocol it works with and architecture of this protocol usage (for instance, REST). In our case it is HTTP or HTTPS.

Client is a way to simplify a whole work with sending and obtaining data, because:

  • it takes into account all protocol specification
  • it is designed to be high performance at low level and simple to use at hign level
  • developer doesn’t need to know how protocol works

HTTP Request Methods

HTTP Requests are widely used to work with external databases by means of API. In order to make API usage more predictable and easy requests are divided by methods:

  • GET – to pass info in url (e.g https://[site url][parameters]) and obtaine public data
  • POST – to pass private info
  • DELETE – to pass private info in order to request removal of something from database
  • PUT – to pass private info in order to request update of something in database
  • and others

HTTP Client examples

webapplication

.NET C# HTTP Client

System.Net.Http namespace provides a lot of classes for work in web. HttpClient is a modern and fast HTTP .Net Client.

...
   using System.Net.Http;  //adding Http namespace
...
   HttpClient _client=new HttpClient();

   string url="http://www.brainbeast.best";  //some url

   string resp = await _client.GetStringAsync(url);  //get request, resp will contain html code of a webpage
...
Javascript HTTP Client

Web version of JS has XMLHttpRequest object that can send and recieve http messeges, set request method and work in async regime.

...
    function PostRequest() {
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function () {
        if (this.readyState == 4 && this.status == 200) {
            
          var resp=this.responseText;  //response messege

        }
    };
    
    xhttp.open("POST", "http://someurl", true);  //setting Method, url, async regime (true/false)
    xhttp.send("private data");  //private data to send
}
...
PHP HTTP Client

By means of cURL PHP running servers can execute http requests. cURL is not the only option.

...
    $crl = curl_init();

    curl_setopt($crl, CURLOPT_URL,"https://some_url");

    curl_setopt($crl, CURLOPT_POST, 1);

    curl_setopt($crl, CURLOPT_POSTFIELDS,"private post data");

    curl_setopt($crl, CURLOPT_RETURNTRANSFER, true);

    $resp = curl_exec($crl);

    curl_close ($crl);

    echo $resp;
...

How to download image with HTTP Client on C#?

...
   HttpClient _client=new HttpClient();
   byte[] buffer = null;
   try
   {       
      HttpResponseMessage task = await _client.GetAsync("https://www.brainbeast.best/wp-content/uploads/2020/05/client-server.jpg");
      Stream task2 = await task.Content.ReadAsStreamAsync();
      using (MemoryStream ms = new MemoryStream())
      {
        await task2.CopyToAsync(ms);
        buffer = ms.ToArray();
      }
      File.WriteAllBytes("C:/client-server.jpg", buffer);  //path to save image and bites of this image
   }
   catch
   {

   }
...

How to set timeout in C# HTTP Client?

...
   HttpClient _client=new HttpClient();
   _client.Timeout = TimeSpan.FromSeconds(300);
...

How to add Custom Header to HTTP Request on C#?

...
   HttpClient _client=new HttpClient();
   client.DefaultRequestHeaders.Add("Some header", "some value");
...

How to create HTTP POST request and send JSON data on C#?

First option is to install Microsoft.AspNet.WebApi.Client from Nuget.

...
class Person
{
   public string Name {get;set;}
   public string Surname {get;set;}
}
...
   Person person=new Person {Name ="Andy", Surname="Smith"};  //you can post any class with public fields

   HttpClient _client=new HttpClient();

   var response = await client.PostAsJsonAsync("http://endpoint", person);

   var resptext = await response.Content.ReadAsStringAsync();
...

Second option:

...
   HttpClient _client=new HttpClient();

   var values = new Dictionary
      {
        { "username", "andy" },
        { "password", "12345678" }
      };

   var content = new FormUrlEncodedContent(values);

   var response = await _client.PostAsync($"https://some endpoint", content);

   var responseString = await response.Content.ReadAsStringAsync();
...

Derivation of Friedmann Theory by means of Classical physics

The relevance of cosmology

On account of technology deficiency specialists were unable to inspect far space in detail before XX century. By the end of the XIX century telescope had been already invented. Due to its small accuracy the appliance could recognize form, color and trajectory of the nearest astronomical bodies: planets, coments, Moon, Sun and so on. To see things distintcly off the solar system was impossible. Nevertheless, scientists didn’t watch the world go by (figuratively). Bit by bit they had created a theory that became the first ground of rational worldview. It was called Classical physics. It was founded by Johannes Kepler, Galileo Galilei and Isaac Newton in the first half of XVII century. Universe was shown as a set of massive material bodies and actions on them in accordance with Law of universal gravitation. But still scientists were beset by the questions because a large range of things was unexplained. The next step of worldview development was Classical electrodynamics founded by James Maxwell, Hans Christian Oersted, Jean-Baptiste Biot, Felix Savart, Michael Faraday, Simeon Denis Poisson and Andre-Marie Ampere during the XIX century. Laws of charge interference modified once viewpoint: electrostatic and magnetic forces added to the gravitational. Their theory landed a hand to predict existence of electromagnetic waves that spread with a speed of light (that is electromagnetic wave). Besetting temptation of Albert Michelson and Edward Morley to check the last thing leaded to the emergence of a brand new hipotesa. By means of Michelson-Morley experiment the ideas about all-encompassing ether were broken. Hendrik Lorentz decided to look through the result of experiment outcome: the speed of light was the same at every frame of reference. Thus he derived the transformation of the physical parameters in order to clarify the outcome, – Lorentz transformation. It was a fundament of Einstein’s special theory of relativity. That turned to be a moment of a revolutional step from classical physics to Einstein’s relativistic mechanics. The accuracy of cosmological specification and predictions become better. At the same period of time the other part of physics had been developing, thermodynamics. Sadi Carnot, James Joule, Willard Gibbs and Ludwig Boltzmann were resposible for it during ХIХ-XX centuries. Rich knowledge about energy transformation appeard to be useful for theories about Universe. Afterall, the time of two crucial theories began: Quantum mechanics and General theory of relativity. We should pay tribute to the electrical science. It gives us an opportunity to look into the most appealing scene from Earth – the space.

Nowadays telescopes can recognize a particle of sand moving round the Sun. They work with all diapasons of waves. Detectors are able to trace all kinds of radiation, nuclear fragments and neutrino.

In 2016 LIGO detectors cought gravitational waves. Modern technologies let us strike up an acquaintance with unordinary astronomical events and bodies: dark holes, neutron stars, giant stars, far galaxies, clusters and superclusters of galaxies. Today is the best time to discover cosmos. It is a clue for further development of physics by two reasons: it is no longer necessary to make experiments on Earth and there are always new discoveries on the way of research.

Universe in Newton’s physics

Newton’s theoreme

Let there be a sphere with a center in point O. If the sphere is filled with mass isotropically in all directions, gravitational force, that would influence point A outside of the sphere, is equal to the force that would enfluence point A as if all mass is concentrated in point O.NewtonsTheorem


Newtons Theorem Illustration
Consequence: if point A is located inside the sphere on the distance of R2 from O, then gravitational force that would influence point A is equal to the force that would influence A as if all mass is concentrated in point O.

The law of The Universe expansion (Hubble’s law)

Euclid space

Let’s make an observation of The Euclidean space:

  1. It is isotropically filled with galaxies (white dots)
  2. Let`s set galaxy A as a beginning of the reference system and write down X-axis
  3. Mark galaxy B at X1 point

Then the distance between A and B is L=x*a(t).

α – the cosmological scale factor that depends on time. It is a parameter of world expansion.

By means of formula for L the speeds of galaxies is found as the derivative of L: v=L`=x*a`(t), where x does not change with time ( i.e, constant).
Edwin Hubble proven the relation between Redshift of far galaxies and their speeds in 1929.

The derivative of scale factor with timescale factor derivative
Hubble ConstantHubble constant
Speed of galaxies in x distancevelocity

The relation between Light parameters and space expansion

Photon is one of the most abundant particle in the Universe. Current researches show that there are ten photons per one proton. Meaning that the corpuscles of light outnumber corpuscles of matter. The main properties of an electromagnetic wave are Energy (E), frequency (v), wavelength (λ) and the speed of light (c).

E=v*h,  где h – постоянная Планка; v=c/λ;

Let’s write the wave of light in space: wave By the Hubble’s law the wavelength changes with time like that:Lambda wave-and-lambda
Then frequency v ∝ 1/a. So far as the energy is straight proportional to frequency, E ∝ 1/a. That is why the energy of each quantum of light decreases with time.

Cosmological model by Newton’s physics

Let the mass of galaxy B equals m. Let’s use the Newton’s theorem and mark that the mass of matter in radius X1 equals M. Thus we can think that point O has all the mass M. Write down the law of conservation of energy for closed system (i.e., Universe). law of conservation of energy
The first member is kinetic energy, the second is potentional.
Multiply both sides of the equation by 2:
two-times
Two times constant is some other constant, therefore const*2=const;
Divide both sides of the equation by m (as m is a constant, const/m=const):
divide-by-m
Let’s substitute L and v taken from Hubble’s law to the equation:
substitution
Then rewrite M as ρ*V (density * volume); density is the same everywhere in our world model, volume is found like:second-substitution
Eventually, the equation transforms to: G-substitution
Density and α depend on time and do not depend on position. (4п/3 * G) – constant. x and, consenquently, x^2 are unic and they depend on position. The left side of the equation is proportional to x^2, so the right side should be proportional to x^2 too. Thus we can simplify the equation: divide-by-x
Now there is no relation of A,B positions and chosen O (reference frame). The only thing it depends on is α that changes with time.
Let’s transform the equation:

  1. Transfer the second member from the left side to the right
  2. Divide both sides by a^2
  3. Constant divided by a^2 should be overwritten with k

result

Friedmann–Robertson–Walker equation

Density is in relation with time because space expands with as it flows. Imagine the region with volume that equals the unit of distance measurement in third power. Sizes change in relation with cosmological scale factor, therefore volume equals (1 length unit * a)^3. Rewrite density as a constant mass of a region divided by the volume: ρ= M/(1*a^3 ) (units of measurement are neglected). Finally, the equation looks like:
new-equation

k may be both positive and negative value, minus (before k) is just a historical tradition.

Solution of Friedmann–Robertson–Walker equation

Matter-dominated era.

equation
Equation works if the abundant part of matter-energy is in a state of matter (not radiation: photons, neutrino, any particles that move almost with a speed of light). Otherwise mass of a nominal region M would be able to change. For instance, photons always move with a speed of light because their rest masses are zero. Also all material objects should be preserved in nominal regions in order to keep M constant. That is why the previous equation works during Matter-dominated era. The set of solutions depends on values of unknoun parameters. The simplest option is k=0:
zero-k
α may depend on time differently: in inverse ratio, in direct ratio, exponentially and so on. Let’s suppose that dependency:

α = c*tp, тогда a’=c*p*t(p-1)

Let’s make the substitution:
substitution-two
We can see that the power of time is two on the left side and 3p on the right side. Therefore 2=3р, р=2/3. Then a=ct^(2/3). Substitute p to the equation:substitution-three-ru
That nonlinear dependence means that space expands with acceleration. Mark that scale factor equals zero at zero point of time when density of matter was infinite.
Let’s rewrite Hubble’s constant formula: substitution-eng

Let’s suppose that border surfaces of a unit (with mass M) are perfectly elastic. Then the pressure, that exerts by mass M on surfaces, equals zero, because mass does not move through units.

Radiation-dominated era

Microwave Sky

The abundant part of matter-energy was in a state of high energy radiation before Matter-dominated era. It was clear by the evidence of orbital observations of WMAP – Wilkinson Microwave Anisotropy Probe. The spacecraft was detecting microwave light from all directions. This data was explained by the fact that the energy of high frequency radiation (born during eairly period of Universe evolution or after The Big Bang) was decreasing because of the influence of scale factor. On a small scale these microwaves isotropically filled the space. They were called Cosmic microwave background aka relic radiation. This period was named Radiation-dominated era. We has noticed the region with volume Х^3 and mass M. Now, let’s imagine that units (regions) are filled with radiation. Then the streams of light will be moving througn unit but the average amount of photons in unit is constant. As long as the wavelength is increasing with time, the average energy of photons is not a constant, it is some constant divided by scale factor: E ∝ d/a. Let’s find density of photon energy dividing the frevious equation by the volume of a unit (Х*a(t))^3, where X is supposed to be equal 1: E/V ∝ d/(a^4). This means that the density of photon matter is proportional to 1/(a^4), not 1/(a^3). Let’s change Fridman’s equation to match new conditions: 2=4*р. Then р=1/2:

c-param
scale-factor
new-hubble-constant

Check the case when light move by X-axis in its unit, that has perfectly elastic borders. The force, that acts upon borders, equals: F=dP/dt, multiply the numerator and denominator by the speed of light: F=(dP*c)/(dt*c)=E/L. Divide both sides by the contact area of ​​light with border: F/S=E/(L*S) => P=E/V, where E/V let’s call a density of enegry and denoteρE. Actually light is moving through all axes (X, Y, Z). Therefore general pressure on unit is: P2= ρE/3.

Dark energy-dominated era

Imagine a one-dimensional world with Newton’s law of universal gravitation and Hubble’s law. It isotropically filled with matter in the form of galaxies, clusters, stars and so on. Let us depict it as follows: galaxies galaxies-scoped All Matter in Universe is colored yellow. A, B – galaxies. Length L between A and B are increasing by Hubble’s law. Accordingly, the density of the substance will decrease. Let’s look at empty space between A and B – it is expanding.

Meaning that there are abstract objects, the space made of, appearing continuously. Their density is the same in any point of the Universe and it does not change with time. This effect is called vacuum energy or dark energy.
α (scale factor)P (pressure)
ct^(2/3)0
ct^(1/3)P=ρE/3

Try to find out the dependence of P on a variable w: P=w*ρE

dependence on αP (pressure)w (some variable )
ρ ∝ 1/(a^3)00
ρ ∝ 1/a^4ρE/31/3
Firstly, let’s define w for energy density that does not change with time. Rewrite equation where the enegry of a unit is decreasing while volume is increasing: dE=-PdV. If E=ρV (ρ-energy density), then ρdV+Vdρ= > ρdV+Vdρ=-PdV = > Vdρ=-(P+ρ)dV. So far as P=w*ρE, Vdρ=-(w+1)ρdV. Transform the equation: dρ/ρ=-(w+1)dV/V. dIn(f)=df/f – logarithmic derivative. Then: dIn(ρ)=-(w+1)*dIn(V)= >In(ρ)=-(w+1)*In(V)+C=> => In(ρ)=In(1/V^(w+1) ) +C=> ρ ∝ 1/V^(w+1), V=X^3 * a^3 = 1*a^3 => ρ ∝ 1/a^(3(w+1)). If ρ is constant, w=-1 (a0 =1); P=-ρE. What means that energy density is negative value because pressure is positive on account of world expanding. Rewrite Fridman’s equation: overwrite

8п*ρ*G = Λ, где Λ – cosmological constant; H= √(Λ/3)

a`=a√(Λ/3). Let’s find intergral of a’. The only function does not change after differentiation is exponent: a ∝ exp⁡(Ht). Period of Universe evolution with exponential dependence of scale factor from time is called Dark energy-dominated era.

Conclusions

Every one of third equations makes sence in any period of universe evolution. But practical influence will be different for each period. We has found out that there are tree things that affect on world expansion: relatively static matter, radiation and dark matter. In the beginning density of radiation energy had the highest value among others. That is why a ∝ t^(1/2) had the most efficient influence. Over time wavelengths of radiation were decreasing by Hubble’s law (energy and density of energy too). Then matter energy density became the highest. After matter pieces being moved from each other its density was comparable with vacuum energy density and the most important dependence was a ∝ exp(Ht).

References

FAQ about Regular expressions with examples

regex
Regular expressions are both textual data processing method and language for building masks. Mask is a set of symbols and special characters that represent some pattern, which is used to catch segments of text for further work (replacing, extracting, checking conditions).
Regular expressions can be used with many popular programming languages: C#, javascript, pyton, php and so on.

Note: rules of building masks are similar in almost all cases.

How to use regular expression on C#?

To set up the regular expression tool, three major steps should be performed:

  1. Create pattern string
  2. Create exemplar of Regex class
  3. Process text by means of Regex
...
   string [email protected]"\sHello{3}";  //mask for catching "hello" with three O in the end and one space in left side
...

Note: pattern or mask is rule of processing text. You can try any mask using online tester.

...
   Regex reg = new Regex(pattern);  //Regular expression
   
   string text="Hi, John. Hellooo, Mary. Hello, Gary.";  // textual data to analize

   MatchCollection matches = reg.Matches(text);  //returns collection of Matches. In our case it matches " Hellooo"
...

It is possible to call a match by index from matches. Match class has some usefull properties:

  • Value – string that matches pattern
  • Index – position of the first character of captured string in the original string
  • Length – number of character in captured string
...
   string firstmatch=matches[0].Value; //firstmatch equals " Hellooo"
...

How to create list of substrings from text with Regex?

...
   MatchCollection matches = reg.Matches(text);  //match collection from text

   List resultlist = matches.Select(m => m.Value).ToList();  //using Linq

   string[] resultarray= new string[matches.Count];  //not using Linq
   for(int i=0; i<matches.Count; i++)
   {
      resultarray[i] = matches[i].Value;
   }
...

How to ignore letter case in regular expressions?

...
   Regex regex = new Regex(pattern, RegexOptions.IgnoreCase); //ignores letter case

   Regex regex2 = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.RightToLeft); //ignores letter case and process from right to left
...

Note: it is possible to set more RegexOptions (RegexOptions.IgnoreCase | RegexOptions.RightToLeft | RegexOptions.Compiled).

How to do replacing with Regex?

...
   //replaces "Helooo" with "Howdy"
   string newtext = Regex.Replace(text, pattern, "Howdy");
...

How to set timeout to Regex?

...
   Regex regEx = new Regex(pattern, RegexOptions.None, TimeSpan.FromSeconds(10));  //stops processing after 10 sec
...

How to make Regex work faster?

Dealing with huge amount of textual data, you will stumble upon a problem of execution speed loss. Setting RegexOptions.Compiled will make Regex perform faster.

...
   Regex regEx = new Regex(pattern, RegexOptions.Compiled);  //takes a bit more time for compilation, but works faster
...

Pattern for extracting emails from text

The following mask is able to catch ordinary emails (e.g. [email protected]), complex emails ([email protected]) and protected from spam emails (e.g. email[at]email.com, [email protected] dot com):

...
   string emailpattern =  @"([a-zA-Z0-9_\-\.]+)(@|(\s*\[\s*at\s*\]\s*)|(\s*" + "\"" + @"\s*(at)\s*" + "\"" + @"\s*))([a-zA-Z0-9_\-\.]+)((\.)|(\s+dot\s+)|(\s*\[\s*dot\s*\]\s*)|(\s*" + "\"" + @"\s*(dot)\s*" + "\"" + @"\s*))([a-zA-Z]{2,5})";
...

Patterns for extracting links from page

...
   string tagPat = @"?<\s*a[^>]*>(.*?)?<\s*/\s*a\s*>";  //pattern for A tags 

   string hrefPat = "href *= *(\"(?<url>.+?)\")"; //pattern with group url for href attribute
...

Groups in regular expressions

You can organize result of Regex matches into one or more groups:

... 
   string hrefPat = "href *= *(\"(?<url>.+?)\")"; //pattern with group url for href attribute
   
   string text = "<a href=\"http://somedomain\"> Domain </a>";

   Regex regex = new Regex(hrefPat, RegexOptions.IgnoreCase);

   string result=regex.Match(text).Groups["url"].Value;  //result equals http://somedomain
...

FAQ about Selenium WebDriver on c# with examples

Selenium is a set of tools that are used to automate web browser work. It is possible to use these tools with such popular languages as C#, PHP and Java.

How to install Selenium tools to the project in Visual Studio?

First of all, it is necessary to install Selenium.Webdriver packages to the project. By means of NuGet package manager programmer can install them right from the project window:

nuget

  1. Go to Tools/NuGet Package Manager/Manage NuGet Packages for Solution…/ in Visual Studio
  2. In “NuGet – Solution” tab go to Browse
  3. In search line write the package name – Selenium.Webdriver
  4. Choose the package and press install button

Secondly, it is necessary to download drivers that match browsers you choose to test and OS of the computer:

  1. Update all browsers you want to test
  2. Download appropriate driver
  3. Place downloaded driver (.exe) to the application directory

Finally, using directives to Selenium namespaces must be added:

...
   using OpenQA.Selenium;
   using OpenQA.Selenium.Chrome;
   using OpenQA.Selenium.Firefox;
...

Note: Webdriver won`t work with Firefox browser if Firefox: Developer Edition is not installed.

How to create an instance of WebDriver on c#?

There are some classes for each webdriver:

  • ChromeDriver
  • FirefoxDriver
  • and so on

Also, all of them inherit IWebDriver interface. An instance of webdriver class can be initialized by means of default constructor (that has some overloads):

...
   FirefoxDriver firefox = new FirefoxDriver();
   ChromeDriver chrome = new ChromeDriver();
   List<IWebDriver> drivers = new List<IWebDriver>() { firefox, chrome };  //put webdrivers to the list
...

How to force webdriver to wait until the web content is fully loaded?

Working with webdriver, programmer can stumble upon such a problem that webdriver instance won`t wait until web page content is fully loaded. That can lead to exceptions when command to find and perform some actions with dom element is executed. To solve this issue Implicit wait time should be set.

...
   IWebDriver driver = new ChromeDriver();
   driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10); //driver will try to execute dom commands for 10 seconds in case of failure
...

How to hide command prompt (console) while using webdriver on c#?

...
   ChromeDriverService chromeservice = ChromeDriverService.CreateDefaultService();
   chromeservice.HideCommandPromptWindow = true; //hide console window
   ChromeDriver driver = new ChromeDriver(chromeservice);

   FirefoxDriverService firefoxservice = FirefoxDriverService.CreateDefaultService();
   firefoxservice.HideCommandPromptWindow = true; //hide console window
   FirefoxDriver driver = new FirefoxDriver(firefoxservice);
...

How to execute your javascript code in webdriver window?

There are two similar options to inject and execute new javascript code in webdriver:

  • by means of webdriver class method ExecuteScript or ExecuteScriptAsync
  • by means of IJavaScriptExecutor interface that is inherited by webdriver classes
...
    FirefoxDriver firefoxdriver = new FirefoxDriver();
    firefoxdriver.ExecuteScript("alert("Hello!");");  //your js code
    firefoxdriver.Quit();

    IWebDriver driver= new FirefoxDriver();
    IJavaScriptExecutor executor = driver as IJavaScriptExecutor;
    executor.ExecuteScript("alert("Hi!");");   //your js code
    driver.Quit();
...

How to move cursor to some point in webdriver window?

In case of web pages what contain actions associated with mouse moves, ability to manipulate mouse is needed. For example, in order to parse web pages, you will need to get whole usefull html content from web sites. As a rule, for a number of reasons (often for protecting from spam bots) web masters try to hide some part or a whole sites` content from beeing loaded by bots (in our case by webdriver bot). The most popular way of achieving such kind of a protection is to check if the mouse is moved while surfing the site. Sometimes the folowing code can be really helpful:

...
   ChromeDriver driver = new ChromeDriver();
   Actions action = new Actions(driver);

   action.MoveByOffset(5,5).Perform();  //moves cursor to point (5,5)

   action.MoveByOffset(10, 15).Perform();  //moves cursor to point (10,15)
...

How to navigate to some webpage in webdriver window?

...
   driver.Navigate().GoToUrl("https://www.brainbeast.best/");
...

How set page load time?

...
   driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(10);  //time to wait untill page is loaded
   try
   {
      driver.Navigate().GoToUrl(url);  //if page is not loaded in 10 sec exception is thrown
   }
   catch
   {
      //catch timeout exception
   }
...

How to set Chrome command line switches?

In case of making complex and various tests chrome CLI switches can be really useful and sometimes required.

...
   ChromeOptions options = new ChromeOptions();
   options.AddArguments(new List<string>() { "switch 1", "switch 2", "and so on" });  //adding list of chrome CLI arguments

   ChromeDriver driver = new ChromeDriver(options);
...

Full list of CLI agruments.

How to set windowless regime in webdriver?

If tests are done without requirement of showing up any useful information it is logical to switch off window mode. To run webdriver without window, add neseccary CLI arguments:

...
   ChromeOptions options = new ChromeOptions();
   options.AddArguments(new List<string>() { "headless"});
...

Drag-and-Drop on C# in WPF

working-womanDeveloping desktop WPF or WinForm projects, you can stumble upon an idea of creating drag & drop functionality. Drag-and-Drop effect will simplify file manipulation between browser, computer file system and desktop applications.

To understand the following sample you should already know:

  1. Basic priciples of Object-oriented programming
  2. How to create WPF or WinForm projects

To make it work you should get on with installed Visual Studio.

Drag & Drop in WPF .Net Core/Framework

  1. Create WPF project
  2. Add ListView to the main window
  3. Add Drop and MouseMove handlers to ListView

wpf-project

In order to simplify the logic of the program, let`s add a class and name it FileObject, that contains Name (image.jpeg, essay.txt etc) and Fullname (C:/somefolder/image.jpeg, E:/myessays/essay.txt etc):

...
    class FileObject
    {
        public string Name { get; set; }

        public string FullName { get; set; }

        public FileObject(string name, string fullname)
        {
            Name = name;
            FullName = fullname;
        }

        public override string ToString()
        {
            return Name;
        }
    } 
...

By means of the following code user is able:

  • to drop some file to the list.
  • to drop some file to some folder from list

Dragging the file to ListView, it is possible to handle drop event and get the full file`s path:

...
     public readonly string DirPath = AppDomain.CurrentDomain.BaseDirectory;      /*path to the folder where the executable file is*/

        private void lv_list_Drop(object sender, DragEventArgs e)
        {
            string[] file_pathes = (string[])e.Data.GetData(DataFormats.FileDrop);  /*file_pathes contains pathes of dragged selected files*/

            //add every file to the list copping them to the application directory
            foreach (var path in file_pathes)
            {
                string new_path = DirPath + Path.GetFileName(path); //New file path

                File.Copy(path, new_path);                         //copping dragged file by new_path

                lv_list.Items.Add(new FileObject(Path.GetFileName(path), new_path));  //adding FileObject (that stores file info) to the list
            }
        }
  
...

Note: ListView can put any object to its items property. To show textual representation of the items ListView calls method ToString() that is marked as virtual in object class. That is why ToString() is overridden in FileObject, to show short file name in the list.

To drag some file from the list is it necessary to select it and put cursor in some folder (or browser window etc) realising the left button of the mouse:

...
        private void lv_list_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (e.MouseDevice.LeftButton==System.Windows.Input.MouseButtonState.Pressed) //Checking that leftbutton is pressed
            {
                var file = lv_list.SelectedItem as FileObject; //Taking the selected object as FileObject

                if(file!=null) //Check that item is really selected. Alternatively file variable equals null

                DragDrop.DoDragDrop(this, new DataObject(DataFormats.FileDrop, new string[] { file.FullName }), DragDropEffects.Move);/*new string[] {...} - array contains files` pathes user wants to drag from list*/

                lv_list.Items.Remove(file);                   //removing dropped obect from the list
            }
        }
...

Drag & Drop in WinForm .Net Core/Framework

Instructions are almost similar to the previous ones:

  • Create project
  • Choose Control element (ListView, DataGridView, ListBox etc)
  • Add DragDrop and MouseMove handlers
...
    private void YourElementControl_DragDrop(object sender, DragEventArgs e)
    {
       ...
       foreach (string path in (string[])e.Data.GetData(DataFormats.FileDrop))
            {
                File.Copy(path, DirPath + Path.GetFileName(path));
            }
       ...
    }

    private void YourElementControl_MouseMove(object sender, MouseEventArgs e)
    {
     ...
         if (e.Button == MouseButtons.Left)
         {
                 DoDragDrop(new DataObject(DataFormats.FileDrop, new string[] { PathToFirstFile,PathToTheNextOne }), DragDropEffects.Move);
         }
     ...
    }
...

Note: It is possible to implement drag-and-drop for both files and folders. Also it is possible to use various Control elements for implementation.