Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Dear Experts ,

Please I Created AX Component with C# and Compiled and it was ready to use
i test it and thing was doing great i formate my laptop and then bomb not working . i do this thing after formate :

1- Compiling command
csc /t:library AXComponent.cs /r:System.Drawing.dll /r:System.Drawing.dll /r:System.dll  


2- Register command
regasm AXComponent.dll /tlb 


3- change internet security, i change all related to activeX options to enable.

before i change the internet security the Error Was :
<pre lang="msil">
Message: Automation server can't create object
Line: 170
Char: 9
Code: 0
URI: http://localhost:49208/Janaderia%20Last/MainSite/GatePassCardPrinting.aspx



after i change the setting he give me exception message
unknown exception


when i trace the error and add it to watch the error is
new ActiveXObject("PrintingComponent.AXComponent")  Unable to evaluate the expression. The system cannot find the file specified.



Code Behind:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using System.Runtime.InteropServices;
using System.Drawing.Drawing2D;
using System.ComponentModel;
using System.IO;
using System.Drawing.Text;
using System.Drawing.Imaging; 
namespace PrintingComponent2
{
    public interface IAXComponent2
    {
        string _name { get; set; }
        string _work { get; set; }
        string _job { get; set; } string _nationality { get; set; } string _commite { get; set; }
        string _dateOfExpire { get; set; } string _borderColor { get; set; } string _PersonalImage { get; set; }
        int _janaderiaNo { get; set; }
        Image _image { get; set; }
        string _Message { get; set; }
        string _serverName { get; set; }
        string Print(string serverName, string name, string work,
           string job, string nationality, int janaderiaNo, string commite,
            string dateOfExpire, string borderColor, string PersonalImage, string ID);
        void doc_PrintPage(object sender, PrintPageEventArgs e);
    }
    [ClassInterface(ClassInterfaceType.AutoDual)]
    public class AXComponent2 : IAXComponent2
    {
        public string _serverName { get; set; }
        public Image _image { get; set; }
        public string _name { get; set; }
        public string _work { get; set; }
        public string _job { get; set; }public string _nationality { get; set; }
        public string _commite { get; set; }
        public string _dateOfExpire { get; set; }public string _borderColor { get; set; }
        public string _PersonalImage { get; set; }
        public int _janaderiaNo { get; set; }
        public string _ID { get; set; }
        public string _Message { get; set; }
        public string Print(string serverName, string name, string work,
            string job, string nationality, int janaderiaNo, string commite, string dateOfExpire,
            string borderColor, string PersonalImage,string ID)
        {
            _name = name; _work = work; _job = job; _nationality = nationality; _commite = commite;
            _dateOfExpire = dateOfExpire; _borderColor = borderColor; _PersonalImage = PersonalImage;
            _janaderiaNo = janaderiaNo;
            _serverName = serverName;
            _ID = ID;
            string printerName = ReadFromConfig();
            PrintDocument doc = new PrintDocument();
            doc.PrinterSettings.PrinterName = printerName;
            PageSettings ps = new PageSettings();
            PaperSize psize = new PaperSize();
            psize.Height = 5;
            psize.Width = 5;
            ps.PaperSize = psize;
            ps.Margins = new Margins(0, 0, 0, 0);
            doc.DefaultPageSettings = ps;
            doc.PrintPage += new PrintPageEventHandler(doc_PrintPage);
            doc.Print();
            return _Message;
        }
        private string ReadFromConfig()
        {
            TextReader tr = new StreamReader(@"c:\config\config.ini");
            string printerName = tr.ReadLine();
            tr.Close();
            return printerName;
        }
        public static string ToHijriDate(object date)
        {
            {
                DateTime dt = DateTime.Parse(date.ToString());
                try
                {
                    System.Globalization.CultureInfo arCul = new System.Globalization.CultureInfo("ar-SA");
                    return dt.ToString("yyyy/MM/dd", arCul.DateTimeFormat);
                }
                catch (Exception)
                {
                    return dt.ToString();
                }
            }
        }
        public static string GetCurrentHijriYear()
        {
            return ToHijriDate(DateTime.Now).Substring(0, 4);
        }
        private bool DrawPersonalImage(Graphics gr, string PersonalImage)
        {
            try
            {
                Image i = Image.FromStream(
                    System.Net.HttpWebRequest.Create(
                    _PersonalImage).GetResponse().GetResponseStream());
                gr.DrawImage(i, 160, 300, 280, 330);
                return true;
            }
            catch (Exception ex)
            {
                _Message = ex.Message;
                return false;
            }
        }
        private void DrawBorders(Graphics gr, string borderColor)
        {
            gr.DrawRectangle(new Pen(GetColor(borderColor), 60), new Rectangle(9, 9, 630, 980));
        }
        private Color GetColor(string borderColor)
        {
            Color c = System.Drawing.ColorTranslator.FromHtml(borderColor);
            return c;
        }
        private float GetXLocationDraw(string name)
        {
            float namelenght = name.Length;
            return 650 - namelenght * 22;
        }
        public void doc_PrintPage(object sender, PrintPageEventArgs e)
        {
            try
            {
                
                Stream str = System.Net.HttpWebRequest.Create(
                      _serverName).GetResponse().GetResponseStream();
                Image i = _image != null ? _image : Image.FromStream(str);
                Bitmap bm = new Bitmap(i);
                Graphics gr = Graphics.FromImage((Image)bm);
                DrawBorders(gr, _borderColor);
                DrawPersonalImage(gr, _PersonalImage);
                DrawBarCode39(gr,rotateImage((Bitmap)barCode(_ID),90));
                Font font = new Font("Arial", 30);
                Font stringFont = new Font(font, FontStyle.Bold);
                Font fontjan = new Font("Andalus", 30);
                Font stringFontjan = new Font(fontjan, FontStyle.Bold);
                RectangleF rec = new RectangleF(0, 710, 600, 500);
                StringFormat strFormat = new StringFormat();
                strFormat.FormatFlags = StringFormatFlags.DirectionRightToLeft;
                strFormat.Alignment = StringAlignment.Near;
                gr.DrawString(_name, stringFont, Brushes.Black, rec, strFormat);
                rec.Y = 770;
                gr.DrawString(_work, stringFont, Brushes.Black, rec, strFormat);
                rec.Y = 830;
                gr.DrawString(_job, stringFont, Brushes.Black, rec, strFormat);
                rec.Y = 890;
                gr.DrawString(_nationality, stringFont, Brushes.Black, rec, strFormat);
                rec.Y = 170;
                strFormat.Alignment = StringAlignment.Center;
                string janaderiaNoAr = arabicNumber(_janaderiaNo);
                gr.DrawString("Janaderia" + janaderiaNoAr, stringFontjan, Brushes.Black, rec, strFormat);
                rec.Y = 220;
                rec.X = 5;
                gr.DrawString(_commite, stringFont, Brushes.Black, rec, strFormat);
                string hijriYear = arabicNumber(GetCurrentHijriYear() + " هـ");
                rec.Y = 515;
                rec.X = 230;
                gr.DrawString(hijriYear, stringFont, Brushes.Black, rec, strFormat);
                rec.Y = 650;
                rec.X = -250;
                strFormat.Alignment = StringAlignment.Near;
                _dateOfExpire = arabicNumber(_dateOfExpire);
                gr.DrawString(_dateOfExpire, stringFont, Brushes.Black, rec, strFormat);
                gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
                gr.DrawImage((Image)bm, 0, 0);
                Point p = new Point(0, 0);
                bm = rotateImage(bm, 90);
                 ((Image)bm).Save(@"d:\imaa\imaa.jpg");
                e.Graphics.DrawImage((Image)bm, new RectangleF(0, 0, 340, 210));
                _Message = "Order Send To Printer";
            }
            catch (Exception ex)
            {
                _Message = ex.Message;
            }
        }
        private void DrawBarCode39(Graphics gr, Image image)
        {
            gr.DrawImage(image, 45, 320, 50,280);
        }
        private Bitmap rotateImage(Bitmap b, float angle)
        {
            //create a new empty bitmap to hold rotated image
            Bitmap returnBitmap = new Bitmap(b.Height, b.Width);
            //make a graphics object from the empty bitmap
            Graphics g = Graphics.FromImage(returnBitmap);
            //move rotation point to center of image
            g.TranslateTransform((float)b.Height / 2, (float)b.Width / 2);
            //rotate
            g.RotateTransform(angle);
            //move image back
            g.TranslateTransform(-(float)b.Width / 2, -(float)b.Height / 2);
            //draw passed in image onto graphics object
            g.DrawImage(b, new Point(0, 0));
            return returnBitmap;
        }
        private static bool CheckIfNumber(char p)
        {
            if (p == '0' || p == '1' || p == '2' || p == '3' || p == '4' || p == '5' || p == '6' || p == '7' || p == '8'
                || p == '9')
            {
                return true;
            }
            return false;
        }
        public static string arabicNumber(object strval)
        {
            string strVal = strval.ToString();
            string ival = string.Empty;
            int i;
            int x;
            for (i = 0; i < strVal.Length; i++)
            {
                if (CheckIfNumber((strVal[i])))
                {
                    if (strVal[i] == '0')
                        x = 1632;
                    else if (strVal[i] == '1')
                        x = 1633;
                    else if (strVal[i] == '2')
                        x = 1634;
                    else if (strVal[i] == '3')
                        x = 1635;
                    else if (strVal[i] == '4')
                        x = 1636;
                    else if (strVal[i] == '5')
                        x = 1637;
                    else if (strVal[i] == '6')
                        x = 1638;
                    else if (strVal[i] == '7')
                        x = 1639;
                    else if (strVal[i] == '8')
                        x = 1640;
                    else if (strVal[i] == '9')
                        x = 1641;
                    else if (strVal[i] == '/')
                        x = 47;
                    else if (strVal[i] == '\\')
                        x = 92;
                    else
                        x = 160;
                    ival = ival + Convert.ToChar(x);
                }
                else
                {
                    ival = ival + strVal[i];
                }
            }
            return ival;
        }
        public Image barCode(string ID)
        {
            StringFormat strFormat = new StringFormat();
            strFormat.FormatFlags = StringFormatFlags.DirectionRightToLeft;
            strFormat.Alignment = StringAlignment.Center;
            string TypeFaceName = "CCode39";
            string txt = ID; 
            PrivateFontCollection fnts = new PrivateFontCollection();
            fnts.AddFontFile(@"c:\config\ConnectCode39.ttf");
            FontFamily fntfam = new FontFamily(TypeFaceName, fnts);
            Font fnt = new Font(fntfam, 20);
            Bitmap bmp = new Bitmap(250, 55);
            Graphics g = Graphics.FromImage(bmp);
            g.Clear(Color.White);
            SizeF bc = g.MeasureString(txt, fnt);
            Brush br = new SolidBrush(Color.Black);
            g.DrawString(txt, fnt, br, new RectangleF(0, 0, 250, 55), strFormat);//0, 0);
            ImageFormat format = ImageFormat.Jpeg;
            //bmp.Save(@"d:\imaa\imaa.jpg", format);
            return bmp;
        }
    }
     
}


Best Regards & thanx in advance

AB Abulubbad
Posted
Updated 5-Mar-11 20:53pm
v2

1 solution

I found the solution that when i register i should add this Parameter /codebase
regasm AXComponent.dll /tlb /codebase



regards
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900