Thursday, April 17, 2014

Create Identity Card for like Membership in C# windows application










 










 Codebehind C#:


public static  Bitmap CreateFirstCard(string Name,string MembNo,string Mobile,string Phone,
          Bitmap objBmpImage, Image img ,int Width,int Height )
      {
          //Bitmap objBmpImage = new Bitmap(pbPhoto.Width, pbPhoto.Height);

          int intWidth = Width;
          int intHeight = Height;

          // Create the Font object for the image text drawing.
          System.Drawing.Font objFont = new System.Drawing.Font("Arial", 13, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);

          // Create a graphics object to measure the text's width and height.
          Graphics objGraphics = Graphics.FromImage(objBmpImage);

       

          // Create the bmpImage again with the correct size for the text and font.
          //objBmpImage = new Bitmap(objBmpImage, new Size(intWidth, intHeight));


          // Add the colors to the new bitmap.
          objGraphics = Graphics.FromImage(objBmpImage);

          // Set Background color

          //objGraphics.Clear(System.Drawing.Color.White);
          objGraphics.SmoothingMode = SmoothingMode.HighQuality;



          objGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SystemDefault;

          objGraphics.DrawString(Name, objFont, new SolidBrush(System.Drawing.Color.Black), 95, 75, StringFormat.GenericDefault);

          objGraphics.DrawString(MembNo, objFont, new SolidBrush(System.Drawing.Color.Black), 157, 95, StringFormat.GenericDefault);

          objGraphics.DrawString(Phone, objFont, new SolidBrush(System.Drawing.Color.Black), 95, 120, StringFormat.GenericDefault);

          objGraphics.DrawString(Mobile, objFont, new SolidBrush(System.Drawing.Color.Black), 105, 140, StringFormat.GenericDefault);


          System.Drawing.Rectangle rec = new System.Drawing.Rectangle(310, 70, 102, 94);
          objGraphics.DrawImage(img, rec);

          objGraphics.Flush();

          return (objBmpImage);
      }

      public static Bitmap CreateSecondCard(string Address,string DOB,string BG,
          Bitmap objBmpImage, Image img, int Width, int Height)
      {
          //Bitmap objBmpImage = new Bitmap(pbPhoto.Width, pbPhoto.Height);

          int intWidth = Width;
          int intHeight = Height;

          // Create the Font object for the image text drawing.
          System.Drawing.Font objFont = new System.Drawing.Font("Arial", 13, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);

          // Create a graphics object to measure the text's width and height.
          Graphics objGraphics = Graphics.FromImage(objBmpImage);

      

          // Create the bmpImage again with the correct size for the text and font.
          //objBmpImage = new Bitmap(objBmpImage, new Size(intWidth, intHeight));


          // Add the colors to the new bitmap.
          objGraphics = Graphics.FromImage(objBmpImage);

          // Set Background color

          //objGraphics.Clear(System.Drawing.Color.White);
          objGraphics.SmoothingMode = SmoothingMode.HighQuality;



          objGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SystemDefault;

          objGraphics.DrawString(Address.TrimStart(), objFont, new SolidBrush(System.Drawing.Color.Black), 157, 32, StringFormat.GenericDefault);

          objGraphics.DrawString(DOB, objFont, new SolidBrush(System.Drawing.Color.Black), 157, 72, StringFormat.GenericDefault);

          objGraphics.DrawString(BG, objFont, new SolidBrush(System.Drawing.Color.Black), 157, 100, StringFormat.GenericDefault);


          objGraphics.Flush();

          return (objBmpImage);
      }

private void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                SqlCommand cmd;

                if (con.con.State == ConnectionState.Closed)
                {
                    con.con.Open();
                }

                System.IO.FileInfo imageInfo;

               

                da = new SqlDataAdapter("select * from MembershipCard where CardNO='" + cmbMemberNo.Text + "'", con.con);
                ds = new DataSet();
                da.Fill(ds);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    int.TryParse(ds.Tables[0].Rows[0]["Id"].ToString(), out Id);
                    Photocontent = (byte[])ds.Tables[0].Rows[0]["Photo"];
                    ThumbPhotocontent = (byte[])ds.Tables[0].Rows[0]["ThumbPhoto"];

                    pbImage.Image = CommonFunctions.byteArrayToImage(Photocontent);

                    pbPhoto.Image = Properties.Resources.ID_card_3;
                    pbPhoto2.Image = Properties.Resources.ID_card_4;

                    da = new SqlDataAdapter("select SlNo,TITLE,MEMBNO,NAME,ADDR1,ADDR2,ROAD,CITY,PIN,BLOODGRP,PHONE,MOBILE from " +
                                                                               "Members where MEMBNO='" + cmbMemberNo.Text + "'", con.con);
                    ds = new DataSet();
                    da.Fill(ds);

                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        lblMembNo.Text = cmbMemberNo.Text;
                        lblName.Text = ds.Tables[0].Rows[0]["TITLE"].ToString() + " " + ds.Tables[0].Rows[0]["NAME"].ToString();
                        lblAddress.Text = ds.Tables[0].Rows[0]["ADDR1"].ToString() + "\r\n" +
                                          ds.Tables[0].Rows[0]["ADDR2"].ToString() +
                                          ds.Tables[0].Rows[0]["CITY"].ToString() + "\r\n";
                        lblAddress.Text = ds.Tables[0].Rows[0]["ROAD"].ToString() == "" ? lblAddress.Text + ds.Tables[0].Rows[0]["PIN"].ToString() :
                                            lblAddress.Text + ds.Tables[0].Rows[0]["ROAD"].ToString() + " - " + ds.Tables[0].Rows[0]["PIN"].ToString();

                        lblBg.Text = ds.Tables[0].Rows[0]["BLOODGRP"].ToString() == "" ? "" : ds.Tables[0].Rows[0]["BLOODGRP"].ToString();
                        lblMobile.Text = ds.Tables[0].Rows[0]["MOBILE"].ToString();
                        lblPhone.Text = ds.Tables[0].Rows[0]["PHONE"].ToString();
                        lbldob.Text = "";
                    }

                }
                else
                {
                    Id = 0;
                }

                if (txtFileName.Text == "")
                {
                    if (Photocontent.Length == 0)
                    {
                        imageInfo = new System.IO.FileInfo(Path.GetDirectoryName(
                                                                            System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\face_outline.gif");

                        Photocontent = new byte[imageInfo.Length];
                        imagestream = imageInfo.OpenRead();
                        imagestream.Read(Photocontent, 0, Photocontent.Length);

                        ThumbPhotocontent = CommonFunctions.createThumnail(imagestream, 50, 50);
                        pbImage.Image = CommonFunctions.byteArrayToImage(Photocontent);
                    }
                    else if (clearflag == true)
                    {
                        imageInfo = new System.IO.FileInfo(Path.GetDirectoryName(
                                                                           System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\face_outline.gif");

                        Photocontent = new byte[imageInfo.Length];
                        imagestream = imageInfo.OpenRead();
                        imagestream.Read(Photocontent, 0, Photocontent.Length);

                        ThumbPhotocontent = CommonFunctions.createThumnail(imagestream, 50, 50);
                        pbImage.Image = CommonFunctions.byteArrayToImage(Photocontent);
                    }
                    //imagestream.Close();
                }
                else
                {
                    imageInfo = new System.IO.FileInfo(txtFileName.Text);

                    Photocontent = new byte[imageInfo.Length];
                    imagestream = imageInfo.OpenRead();
                    imagestream.Read(Photocontent, 0, Photocontent.Length);

                    pbImage.Image = CommonFunctions.byteArrayToImage(Photocontent);

                    ThumbPhotocontent = CommonFunctions.createThumnail(imagestream, 50, 50);

                    //imagestream.Close();
                }

               

                //contain our picturebox image bytes.
                MemoryStream stream = new MemoryStream();
                //through the instruction below, we save the
                //image to byte in the object "stream".
                //pbPhoto.Image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);

                //pbPhoto.Image.Save(@"E:\Members\Member1.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

                Bitmap bmp = new Bitmap(pbPhoto.Image, pbPhoto.Width, pbPhoto.Height);

                string AppPath = System.IO.Path.GetDirectoryName(
                                 System.Reflection.Assembly.GetExecutingAssembly().Location);

                bool isExists = System.IO.Directory.Exists(AppPath + @"\Members");

                if (!isExists)
                    System.IO.Directory.CreateDirectory(AppPath + @"\Members");

                bmp.Save(AppPath + @"\Members\Member1.bmp");

                Bitmap bmp1 = CommonFunctions.CreateFirstCard(lblName.Text,lblMembNo.Text,lblMobile.Text,lblPhone.Text,
                    bmp, pbImage.Image,pbPhoto.Width,pbPhoto.Height);
               
                bmp1.Save(AppPath + @"\Members\" + lblName.Text  + "1.bmp");

                Bitmap bmp2 = new Bitmap(pbPhoto2.Image, pbPhoto2.Width, pbPhoto2.Height);

                bmp2.Save(AppPath + @"\Members\Member2.bmp");

                Bitmap bmp3 = CommonFunctions.CreateSecondCard(lblAddress.Text,lbldob.Text,lblBg.Text,
                    bmp2, pbImage.Image,pbPhoto2.Width,pbPhoto2.Height);

                bmp3.Save(AppPath + @"\Members\" + lblName.Text + "2.bmp");

                imageInfo = new System.IO.FileInfo(AppPath + @"\Members\" + lblName.Text + "1.bmp");
                Cardcontent = new byte[imageInfo.Length];
                imagestream = imageInfo.OpenRead();
                imagestream.Read(Cardcontent, 0, Cardcontent.Length);


                imageInfo = new System.IO.FileInfo(AppPath + @"\Members\" + lblName.Text + "2.bmp");
                Cardcontent2 = new byte[imageInfo.Length];
                imagestream = imageInfo.OpenRead();
                imagestream.Read(Cardcontent2, 0, Cardcontent2.Length);


                //if (cmbStatus.Text != "Select")
                {

                    cmd = new SqlCommand("MembershipCard_Insert_SP", con.con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    if (Id != 0)
                    {


                        cmd.Parameters.Add("@Opt", SqlDbType.BigInt).Value = 2;
                        cmd.Parameters.Add("@Id", SqlDbType.BigInt).Value = Id;

                    }
                    else
                    {
                        cmd.Parameters.Add("@Opt", SqlDbType.BigInt).Value = 1;
                        cmd.Parameters.Add("@Id", SqlDbType.BigInt).Value = 1;
                    }


                    cmd.Parameters.Add("@CardNO", SqlDbType.NVarChar, 50).Value = cmbMemberNo.Text;
                    cmd.Parameters.Add("@Photo", SqlDbType.Image).Value = Photocontent;
                    cmd.Parameters.Add("@ThumbPhoto", SqlDbType.Image).Value = ThumbPhotocontent;
                    cmd.Parameters.Add("@MembershipCard", SqlDbType.Image).Value = Cardcontent;
                    cmd.Parameters.Add("@MembershipCard2", SqlDbType.Image).Value = Cardcontent2;
                    cmd.Parameters.Add("@CardStatus", SqlDbType.NVarChar, 50).Value = "Card ready";// cmbStatus.Text;

                    cmd.ExecuteNonQuery();

                    MessageBox.Show("Data saved successfully.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                //string template = "Your Membershipcard is ready. Please collect from Office." +
                //                   "\r\n " +
                //                   "General Secretary, Ernakulam Karayogam.";

                //if (con.con.State == ConnectionState.Closed)
                //{
                //    con.con.Open();
                //}

                //cmd = new SqlCommand("select SmsText from SmsMaster where Id=5", con.con);
                //string template = cmd.ExecuteScalar().ToString();

                //string Message = template;

                //System.Diagnostics.Process proc = new System.Diagnostics.Process();
                //proc.EnableRaisingEvents = false;
                //proc.StartInfo.FileName = "iexplore";
                //proc.StartInfo.Arguments = "http://bulksmsalert.in/SendSms.aspx?username=ekmkgm&password=ekmkgm25&to=" + txtMobile.Text + "&from=ekmkgm&message=" + Message + "";
                //proc.Start();

                clearflag = false;
                cmbMemberNo.Text = "";
                txtFileName.Text = "";
                lblName.Text = "";
                lblMembNo.Text = "";
                lblPhone.Text = "";
                lblMobile.Text = "";
                lblAddress.Text = "";
                lbldob.Text = "";
                lblBg.Text = "";
                pbImage.Image = null;
                con.con.Close();


            }
            catch (Exception ex)
            {
                ErrorLog.WriteErrorLog(ErrorLog.GetLogFilePath(), ex);
            }
        }

No comments:

Using Authorization with Swagger in ASP.NET Core

 Create Solution like below LoginModel.cs using System.ComponentModel.DataAnnotations; namespace UsingAuthorizationWithSwagger.Models {     ...