Advertisements

QR Code Generator in ASP.NET Core Using Zxing.Net


Introduction

In this article, we will explain how to create a QR Code Generator in ASP.NET Core 1.0, using Zxing.Net.

Background

I tried to create a QR Code Generator in ASP.NET Core, using third party libraries but in most of the cases codes are not fully supported in ASP.NET Core because of some version issues etc. I searched a lot in Google but finally I found “Zxing.Net” and it is a library, which supports decoding and generating of the barcodes. I had a discussion with MicJahn and came up  with a great solution.

Before reading this article, you must read the articles given below for ASP.NET Core knowledge.

Zxing.Net

A library, which supports decoding and generating of the barcodes (Example: QR Code, PDF 417, EAN, UPC, Aztec, Data Matrix, Codabar) within the images.

Assemblies Required

The assemblies given below are required for QR Code Generator.

using Microsoft.AspNetCore.Razor.TagHelpers;
using System;
using System.IO;
using ZXing.QrCode;

Packages required

We need the packages given below for drawing and creating QR Code Generator.

"CoreCompat.System.Drawing": "1.0.0-beta006",    
"ZXing.Net": "0.15.0"  

C#

QRCodeTagHelper class given below contains QR Code Generator methods etc.

namespace QRCodeApp
{
[HtmlTargetElement("qrcode")]
public class QRCodeTagHelper : TagHelper
{
public override void Process(TagHelperContext context, TagHelperOutput output)
{
var QrcodeContent = context.AllAttributes["content"].Value.ToString();
var alt = context.AllAttributes["alt"].Value.ToString();
var width = 250; // width of the Qr Code
var height = 250; // height of the Qr Code
var margin = 0;

var qrCodeWriter = new ZXing.BarcodeWriterPixelData
{
Format = ZXing.BarcodeFormat.QR_CODE,
Options = new QrCodeEncodingOptions { Height = height, Width = width, Margin = margin }
};

var pixelData = qrCodeWriter.Write(QrcodeContent);

// creating a bitmap from the raw pixel data; if only black and white colors are used it makes no difference
// that the pixel data ist BGRA oriented and the bitmap is initialized with RGB
using (var bitmap = new System.Drawing.Bitmap(pixelData.Width, pixelData.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb))
using (var ms = new MemoryStream())
{

var bitmapData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, pixelData.Width, pixelData.Height),
System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
try
{
// we assume that the row stride of the bitmap is aligned to 4 byte multiplied by the width of the image
System.Runtime.InteropServices.Marshal.Copy(pixelData.Pixels, 0, bitmapData.Scan0,
pixelData.Pixels.Length);
}
finally
{
bitmap.UnlockBits(bitmapData);
}
// save to stream as PNG
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);

output.TagName = "img";
output.Attributes.Clear();
output.Attributes.Add("width", width);
output.Attributes.Add("height", height);
output.Attributes.Add("alt", alt);
output.Attributes.Add("src",
String.Format("data:image/png;base64,{0}", Convert.ToBase64String(ms.ToArray())));
}
}
}
}

Index.chtml

The code given below will display QR Code Generator.

@{
    ViewData["Title"] = "Home";
}

<h2>@ViewData["Title"].</h2>
<h3>@ViewData["Message"]</h3>

A library which supports decoding and generating of barcodes (like QR Code, PDF 417, EAN, UPC, Aztec, Data Matrix, Codabar) within images.

<qrcode alt="QR Code" content="https://rajeeshmenoth.wordpress.com/" />
https://rajeeshmenoth.wordpress.com/

_ViewImports.cshtml

The code Injecting TagHelper given below is in the entire Application.

@addTagHelper "*, QRCodeApp"

project.json

The dependencies given below are required to create QR Code Application.

{
  "dependencies": {
    "Microsoft.AspNetCore.Diagnostics": "1.0.0",
    "Microsoft.AspNetCore.Mvc": "1.1.2",
    "Microsoft.AspNetCore.Mvc.Core": "1.1.2",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
    "Microsoft.AspNetCore.StaticFiles": "1.1.1",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    "CoreCompat.System.Drawing": "1.0.0-beta006",
    "ZXing.Net": "0.15.0"
  },

  "tools": {
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
  },

  "frameworks": {
    "net452": { }
  },

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

  "publishOptions": {
    "include": [
      "wwwroot",
      "web.config"
    ]
  },

  "scripts": {
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
}

Output

QRCode Generator

QRCode Generator

Reference

See Also

You can download other ASP.NET Core 1.0 source codes from MSDN Code, using the links, mentioned below.

Conclusion

We learnt how to create a QR Code Generator in ASP.NET Core 1.0 Using Zxing.Net. I hope, you liked this article. Please share your valuable suggestions and feedback.

Advertisements

Composite Words using C#



Introduction :

A composite word is a concatenation of two-or-more other words, such that the minimum length of each constituent word is 3.

In this article the program which accepts a list of words and an integer or string as input and prints the count of composite words which can be constructed by concatenating integer and string or more other words which are also present in the input.

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace ConsoleApplication3
{
    class Program
    {
        static public void charsorting(char[] process, int length)
        {
            for (int a = 1; a < length; a++)
            {
                for (int b = 0; b < length - a; b++)
                {
                    if (process[b] > process[b + 1])
                    {
                        char tempdata = process[b];
                        process[b] = process[b + 1];
                        process[b + 1] = tempdata;
                    }
                }
            }
        }

        static public bool Permuation(char[] permute, int length)
        {
            for (int c = length - 1; c > 0; c--)
            {
                if (permute[c - 1] >= permute[c])
                    continue;
                else
                {
                    if (c <= length - 3)
                    {
                        char charcheck = permute[c - 1];
                        int value = -1;
                        for (int j = length - 1; j >= c; j--)
                        {
                            if (charcheck < permute[j])
                            {
                                value = j;
                                break;
                            }
                        }
                        if (value == -1)
                            return false;
                        char character = permute[c - 1];
                        permute[c - 1] = permute[value];
                        permute[value] = character;

                        char[] process2 = new char[length - c];
                        for (int d = 0; d < length - c; d++)
                            process2[d] = permute[c + d];
                        charsorting(process2, length - c);
                        for (int e = 0; e < length - c; e++)
                            permute[c + e] = process2[e];
                        return true;
                    }
                    else
                    {
                        char[] dump = new char[3];
                        dump[0] = permute[permute.Length - 3];
                        dump[1] = permute[permute.Length - 2];
                        dump[2] = permute[permute.Length - 1];

                        int count = 3;
                        for (int f = count - 1; f > 0; f--)
                        {
                            if (dump[f - 1] >= dump[f])
                                continue;
                            else
                            {
                                if (f <= count - 2)
                                {
                                    if (dump[f + 1] > dump[f - 1])
                                    {
                                        char ch1 = dump[f + 1];
                                        dump[f + 1] = dump[f];
                                        dump[f] = dump[f - 1];
                                        dump[f - 1] = ch1;
                                    }
                                    else
                                    {
                                        char ch2 = dump[f - 1];
                                        dump[f - 1] = dump[f];
                                        dump[f] = dump[f + 1];
                                        dump[f + 1] = ch2;
                                    }
                                }
                                else
                                {
                                    char ch3 = dump[f];
                                    dump[f] = dump[f - 1];
                                    dump[f - 1] = ch3;
                                }
                                permute[permute.Length - 3] = dump[0];
                                permute[permute.Length - 2] = dump[1];
                                permute[permute.Length - 1] = dump[2];
                                return true;
                            }
                        }
                        return false;
                    }
                }
            }
            return false;
        }

        static void Main(string[] args)
        {
           
            string number;
            int counter = 0;
            string line;

            // Read the file and display it line by line.
            System.IO.StreamReader file =
               new System.IO.StreamReader("E:\\Mvc Test\\ConsoleApplication1\\ConsoleApplication1\\File\\test.txt");
            int count = 0;
            Console.WriteLine("Enter the number :");
            number = Console.ReadLine();
            if (number != null)
            {
              
                while ((line = file.ReadLine()) != null)
                {

                    string s = line + number;
                    char[] buffer = s.ToCharArray();
                    while (true)
                    {
                        Console.WriteLine(buffer);
                        count++;
                        if (Permuation(buffer, buffer.Length) == false)
                            break;
                    }


                }

                Console.WriteLine("\nCount: " + count);
                Console.ReadLine();
                counter++;
                file.Close();


                Console.ReadLine();

           
        }
            else
            {
                Console.WriteLine("Enter atleast one value..!!!");
                Console.ReadLine();

            }


        }
    }
}

File Path:

System.IO.StreamReader file = new System.IO.StreamReader("E:\\Mvc Test\\ConsoleApplication1\\ConsoleApplication1\\File\\test.txt");

Change your file path otherwise you got some error.right click the test.txt inside the file folder.then you get file path.

Output:

Output

 

Reference : Click here

Published : http://www.c-sharpcorner.com/Blogs/49159/

“Login failed for user” C# with SQLConnection


This is the Common Problem we are facing in Server DB Connection in Asp.net.If you are using windows authentication to connect to the Server DB, you need to set “Trusted_Connection=True;” if you are using SQL server authentication, you need to declare User “Id=myUsername; Password=myPassword;”

Code Snippet In Web.Config:

Windows authentication

<add name="windowsConnection" connectionString="Server=localhost; Database=databasename; Trusted_Connection=True;" />

SQL server authentication

<add name="Sqlserverconnection" connectionstring="server=localhost; Database=databasename;uid=db; password=123 "/> 

Versions of C# and .Net framework



C# 2.0 with Visual Studio 2005  —  .NET Framework 2.0

C# 3.0 with Visual Studio 2008  —  .NET Framework 3.0

C# 4.0 with Visual Studio 2010  —  .NET Framework 4.0

C# 5.0 with Visual Studio 2012  —  .NET Framework 4.5


Origin of the C# Programming language



The name “C sharp” was inspired from musical notation where a sharp indicates that the written note should be made a half-step higher in pitch.This is similar to the language name of C++, where “++” indicates that a variable should be incremented by 1. The sharp symbol also resembles a ligature of four “+” symbols (in a two-by-two grid), further implying that the language is an increment of C++.

Due to technical limitations of display (standard fonts, browsers, etc.) and the fact that the sharp symbol (♯, U+266F, MUSIC SHARP SIGN) is not present on the standard keyboard, the number sign (#, U+0023, NUMBER SIGN) was chosen to represent the sharp symbol in the written name of the programming language.This convention is reflected in the ECMA-334 C# Language Specification.However, when it is practical to do so (for example, in advertising or in box art), Microsoft uses the intended musical symbol.


 

Could not load the assembly ‘App_Web_kh7-x3ka’



On the first line of your aspx page within the <%@Page /> tag

you’ll probably see an attribute like inherits="yourPageClass, App_Web_kh7-x3ka".

Delete the “App_Web_XXXX” part and add the CodeFile attribute pointing to your code behind file:

CodeFile="yourPageFile.aspx.cs"


Passing value to one aspx page to another aspx page



//set a link button,go to event and double click commant argument on 1st page

IMGoBJ.IntId = Convert.ToInt32(e.CommandArgument);
Response.Redirect(“~/Pages/More-News.aspx?Qry=” + IMGoBJ.IntId);

// second page..page load

 objNews.IntId = Convert.ToInt32(Request.QueryString[“Qry”]);

 

 


%d bloggers like this: