示例#1
0
        internal Image generateOneSticker(StickerData stickerData)
        {
            Debug.WriteLine("institution: " + institution);
            Debug.WriteLine("SAVE TO PATH: " + saveToPath);
            try
            {
                string imgName;
                if (institution.Equals(Institution.MTS_ALKAHFI))
                {
                    imgName = "ALKAHFI.png";
                }
                else
                {
                    imgName = "kiis.jpg";
                }
                //get image
                Image Image = Image.FromFile("Resources/" + imgName);
                Debug.WriteLine("ResourceManager result :" + (Image == null ? "TRUE" : "FALSE" + Image.GetType()));

                if (null != Image)
                {
                    //write image
                    PixelFormat pf              = PixelFormat.Format32bppArgb;
                    Bitmap      Canvas          = new Bitmap(STICKER_WIDTH, STICKER_HEIGHT, pf); //This is the Bitmap Image; you have not yet selected a file,
                    Brush       BackgroundBrush = new SolidBrush(Color.White);                   //Bitmap BM = new Bitmap(Image.FromFile(@"D:\Resources\International\Picrofo_Logo.png"), rect.Width, rect.Height);

                    Graphics g = Graphics.FromImage(Canvas);

                    //draw background
                    g.FillRectangle(BackgroundBrush, 0, 0, STICKER_WIDTH, STICKER_HEIGHT);

                    //draw logo
                    g.DrawImage(Image, 3, 2, 60, 60);

                    //draw content
                    //g2d.setColor(Color.BLACK);

                    float stringXIndex = STICKER_HEIGHT + 3;
                    //Institution
                    String institutionAlias = getInstitutionAlias(stickerData.institution);
                    //if (institutionAlias.length() <= 17)
                    //{
                    //    g2d.setFont(new Font("Arial", Font.BOLD, 13));
                    //}
                    //else {
                    //    g2d.setFont(new Font("Arial", Font.BOLD, 10));
                    //}
                    Brush fontBrush = new SolidBrush(Color.Black);
                    Font  Font      = new Font("Arial", 8);
                    Font  TitleFont = new Font("Arial", 8, FontStyle.Bold);

                    int reducer = 12;
                    g.DrawString(institutionAlias, TitleFont, fontBrush, stringXIndex, 15 - reducer);
                    g.DrawString(stickerData.itemName, Font, fontBrush, stringXIndex, 32 - reducer);
                    g.DrawString(stickerData.date, Font, fontBrush, stringXIndex, 47 - reducer);
                    g.DrawString(stickerData.code, Font, fontBrush, stringXIndex, 62 - reducer);

                    //draw borders
                    //GENERAL BORDER
                    Pen outerPen   = new Pen(Color.Black, 4);
                    Pen contentPen = new Pen(Color.Black, 1);

                    g.DrawRectangle(outerPen, 0, 0, STICKER_WIDTH - 1, STICKER_HEIGHT - 1);
                    //contents borders
                    int borderXIndex = STICKER_WIDTH - STICKER_HEIGHT;
                    g.DrawRectangle(contentPen, STICKER_HEIGHT, 0, borderXIndex, 18);  //institution
                    g.DrawRectangle(contentPen, STICKER_HEIGHT, 18, borderXIndex, 16); //name
                    g.DrawRectangle(contentPen, STICKER_HEIGHT, 34, borderXIndex, 16); //date
                    g.DrawRectangle(contentPen, STICKER_HEIGHT, 50, borderXIndex, 16); //code

                    string fullPath = saveToPath + "/" + institution + "-" + stickerData.itemName + "_" + count + ".png";
                    Canvas.Save(fullPath, ImageFormat.Png);

                    Debug.WriteLine("SUCCESS SAVING IMAGE:" + fullPath);
                    count++;
                    return(Canvas);
                }

                //var outPutDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
                //var logoimage = Path.Combine(outPutDirectory, "Resources\\ALKAHFI.png");
                //string relLogo = new Uri(logoimage).LocalPath;
                //LinkedResource LinkedResource = new LinkedResource(relLogo);
                //Debug.WriteLine("ResourceManager result :" + (LinkedResource == null ? "NULL" : "FOUND"));// + O.GetType()));
                //if(null!= LinkedResource)
                //{
                //     //Image.FromFile(LinkedResource.ContentLink.LocalPath);
                //}
            }
            catch (Exception e)
            {
                Debug.WriteLine("ERROR :" + e.Message);
            }

            return(null);
        }