Generate Voucher in Asp.Net


Description:-

In this article we will generate voucher from details and display it on your preview part. Here I have created example demo to generate voucher from your entered details and it will save on your directory location.
here we ill create image file for voucher details from bar-code, company logo, Discount offers etc... Here i have set some Style for generating vouchers you can see below in code.

for generating voucher card we need some bar-code images, company logo etc...
Download Required File:- Files

Default.aspx:-

<div>
  <fieldset class="fieldset">
    <legend>&nbsp;<b>Voucher details</b>&nbsp;</legend>
      <div>Voucher Offer
<asp:Label ID="Label1" runat="server" CssClass="contentgeneralfnt"Style="line-height: 25px;"></asp:Label>
      :</div>
      <div>
        <asp:TextBox ID="txtOffer" CssClass="txt" runat="server"></asp:TextBox><br/>
      </div>
      <div style="clear: both; height: 10px;"></div>
      <div class="voucherentryleftdvcontentgeneralfnt">Voucher subtitle :</div>
        <div class="left">
          <asp:TextBox ID="txtSubtitle" CssClass="txt" runat="server"></asp:TextBox>
        </div>
        <div style="clear: both; height: 10px;"></div>
        <div class="voucherentryleftdvcontentgeneralfnt">Voucher terms & conditions :</div>
        <div class="left">
          <asp:TextBox ID="txtTermandCondition" CssClass="txt" TextMode="MultiLine" Height="50px" runat="server"></asp:TextBox>
        </div>
        <div style="clear: both; height: 10px;"></div>
          <div>Voucher website URL :</div>
            <div class="left">
               <asp:TextBox ID="txtWebUrl" CssClass="txt" runat="server"></asp:TextBox><br/>
               <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtWebUrl" Display="Dynamic" SetFocusOnError="True" ErrorMessage="Voucher website URL required!" ValidationGroup="Voucher" CssClass="errormsg"> </asp:RequiredFieldValidator>
            </div>
            <div style="clear: both; height: 10px;"></div>
          <div>
          <img id="imgpreview" runat="server" src="" alt="Voucher's preview" width="425" height="210"/>
        </div>
        <div style="clear: both; height: 10px;"></div>
          <asp:Button ID="btnGenerate" runat="server" Text="Generate Voucher" OnClick="btnGenerate_Click"/>
  </fieldset>
</div>

Default.aspx.cs:-

string logoimgpath = "VouchersImages/Logo/";
string Barcodeimgpath = "VouchersImages/Barcode/";
string BarcodeimgName = "Barcode.png";
string FilePathTOSave = "VouchersImages/Images/";
string ImageNametoSave = "Voucher2.jpg";
string LogoName = "Logo.png";
string CouponCode = "Coupon Code : XXXXX";
string VoucherInfoMsg = "Scan this barcode on your basket payment.";

protected void Page_Load(object sender, EventArgs e)
{}

#region "$$$ Generate Voucher $$$"
protected void btnGenerate_Click(object sender, EventArgs e)
{
  string OfferFullName = txtOffer.Text;
  string TC = txtTermandCondition.Text;
  string OfferSubTitle = txtSubtitle.Text;
  string WEBURL = txtWebUrl.Text;
  GenerateImages_Voucher(OfferFullName, TC, OfferSubTitle, WEBURL);
}

private void GenerateImages_Voucher(StringOfferFullname, StringofferTC, StringofferSubTitle, StringWebURL)
{
  Bitmap objBitmap;
  Graphics objGraphics;
  objBitmap = newBitmap(425, 210);
  objGraphics = Graphics.FromImage(objBitmap);

  //Draw Rectanle with the Black Border
  Pen blackPen = new Pen(Color.FromArgb(162, 3, 14), 2);
  Rectangle rectblack = new Rectangle(0, 0, 425, 210);
  objGraphics.DrawRectangle(blackPen, rectblack);

  Rectangle Boxrect = new Rectangle(2, 2, 421, 206);
  SolidBrush BoxshadowBrush = new SolidBrush(Color.White);
  objGraphics.FillRectangle(BoxshadowBrush, Boxrect);

  FontFamily fontfml = new FontFamily("Arial");
  FontStyle fontbold = FontStyle.Bold;
  FontStyle fontRegular = FontStyle.Regular;

  Font font1 = newFont(fontfml, 20, fontbold);
  Font font2 = newFont(fontfml, 20, fontRegular);
  Font font3 = newFont(fontfml, 9, fontbold);
  Font font4 = newFont(fontfml, 9, fontRegular);

  SolidBrush brush = new SolidBrush(Color.FromArgb(4, 0, 0));
  SolidBrush brushnew = new SolidBrush(Color.FromArgb(162, 3, 14));

  //Offer Name
  Boxrect = new Rectangle(10, 10, 180, 35);
  objGraphics.DrawString(OfferFullname, font1, brushnew, Boxrect);
  
  //Offer Sub title
  Boxrect = new Rectangle(170, 18, 276, 20);
  objGraphics.DrawString(offerSubTitle, font3, brush, Boxrect);
 
  //Barcode
  Rectangle DesRecBarcode = new Rectangle(15, 50, 198, 149);
  Bitmap bmpBarcode;
  Rectangle SrcRecBarcode;
  bmpBarcode = (Bitmap)System.Drawing.Image.FromFile(@System.Web.HttpContext.Current.Server.MapPath(Barcodeimgpath) +   BarcodeimgName);
  SrcRecBarcode = new Rectangle(0, 0, bmpBarcode.Width, bmpBarcode.Height);
  objGraphics.DrawImage(bmpBarcode, DesRecBarcode, SrcRecBarcode, GraphicsUnit.Pixel);

  RectangleDesRec = new Rectangle(345, 10, 70, 53);
  Bitmap bmp;
  RectangleSrcRec;
  bmp = (Bitmap)System.Drawing.Image.FromFile(@System.Web.HttpContext.Current.Server.MapPath(logoimgpath) + LogoName);
  SrcRec = newRectangle(0, 0, bmp.Width, bmp.Height);
  objGraphics.DrawImage(bmp, DesRec, SrcRec, GraphicsUnit.Pixel);

  //Coupon Code
  Boxrect = new Rectangle(210, 50, 150, 20);
  objGraphics.DrawString(CouponCode, font4, brush, Boxrect);

  //Information Message
  Boxrect = new Rectangle(210, 70, 215, 30);
  objGraphics.DrawString(VoucherInfoMsg, font4, brush, Boxrect);

  //T&C
  Boxrect = new Rectangle(210, 105, 215, 80);
  objGraphics.DrawString(offerTC, font4, brush, Boxrect);

  //WebURL
  Boxrect = new Rectangle(210, 180, 220, 20);
  objGraphics.DrawString(WebURL, newFont(fontfml, 8, fontRegular), brush, Boxrect);

  objBitmap.Save(System.Web.HttpContext.Current.Server.MapPath(FilePathTOSave) + ImageNametoSave,System.Drawing.Imaging.ImageFormat.Png);
  objBitmap.Dispose();
  objGraphics.Dispose();

  imgpreview.Src = FilePathTOSave + ImageNametoSave;
}
#endregion

Related Posts

Previous
Next Post »

1 comments:

comments

Thanks for comments.....