0

It works draws text of a number and name on a raffle ticket and saves it in the customer's file correctly. But then returns me = not to the web page but a full black screen with the picture of the ticket. No way to make it go away and get back to the web page except rerun the page. I tried writing to a memory stream but it would not save it correctly if I remove Response.OutputStream and change it to a memory stream in this line = bitmap.Save(Response.OutputStream, ImageFormat.Jpeg);

It took less time to learn how to use the graphic system from scratch than discover how to make it not render to the screen. Lost!

protected string RaffelTicketWrImg(string sOrigBlankTicketImg, string sTicketNbr)
{
    //creating a image object   
    System.Drawing.Image bitmap = (System.Drawing.Image)Bitmap.FromFile(sOrigBlankTicketImg); // set image    
                                                                                              //draw the image object using a Graphics object   
    Graphics g = Graphics.FromImage(bitmap);

    //Set the alignment based on the coordinates      
    StringFormat stringformat = new StringFormat();
    stringformat.Alignment = StringAlignment.Center;
    stringformat.LineAlignment = StringAlignment.Center;
    //stringformat.FormatFlags = StringFormatFlags.DirectionVertical;

    StringFormat stringformat2 = new StringFormat();
    stringformat2.Alignment = StringAlignment.Center;
    stringformat2.LineAlignment = StringAlignment.Center;

    //Set the font color/format/size etc..     
    System.Drawing.Color StringColor = ColorTranslator.FromHtml("#000000");//direct color adding   
    System.Drawing.Color StringColor2 = ColorTranslator.FromHtml("#000000");//customise color adding   
    string sWriteThis = sTicketNbr;//Your Text On Image   
    string sWriteThis2 = sTicketNbr;//Your Text On Image   
                                    //Ticket Size 1.26" by 2.62"
                                    //Graphics g = this.CreateGraphics();

    double dLen = ((sTicketNbr.Length / 2) * 13) / 2;
    int iPixcels = bitmap.Width;
    double iPoints = iPixcels * g.DpiX / Convert.ToInt16(72); //72 / 96;
    int iX_Hroz = Convert.ToInt16((iPoints / 2) - Convert.ToInt16(dLen));
    int iY_Vert = 30;
    g.DrawString(sWriteThis, new Font("arial", 14, FontStyle.Bold),
        new SolidBrush(StringColor), new Point(iX_Hroz, iY_Vert), stringformat); Response.ContentType = "image/jpeg";

    iY_Vert = 222;

    g.DrawString(sWriteThis2, new Font("arial", 14, FontStyle.Bold),
        new SolidBrush(StringColor2), new Point(iX_Hroz, iY_Vert), stringformat2); Response.ContentType = "image/jpeg";

    sWriteThis = txPersonNamed.Text;//Your Text On Image   
                                    //Ticket Size 1.26" by 2.62"
    string[] words = sWriteThis.Split(' ');
    iX_Hroz = 15;
    iY_Vert = 90;

    stringformat.Alignment = StringAlignment.Near;
    stringformat.LineAlignment = StringAlignment.Near;

    foreach (var word in words)
    {
        g.DrawString(word.ToString(), new Font("arial", 10, FontStyle.Bold),
            new SolidBrush(StringColor), new Point(iX_Hroz, iY_Vert), stringformat);
        Response.ContentType = "image/jpeg";
        iY_Vert += 20;
    }

    bitmap.Save(Response.OutputStream, ImageFormat.Jpeg);

    string sSaveFile = Vars.TicketPath + "/" + sTicketNbr + ".jpg";

    if (txEmail.Text.Length == 0) //Test Diaplay
    {
        if (File.Exists(sSaveFile)) File.Delete(sSaveFile);
    }

    bitmap.Save(sSaveFile, System.Drawing.Imaging.ImageFormat.Jpeg);

    // Release memory used by the Graphics object
    // and the bitmap.
    g.Dispose();
    bitmap.Dispose();

    // Send the output to the client.
    Response.Flush();
} 
RBT
  • 18,275
  • 13
  • 127
  • 181
All_IN
  • 1
  • 2
  • What is your question and what are you trying to do? Saving an image to a memory stream works flawlessly. – Charles Feb 28 '20 at 05:15
  • Greetings RBT. The above works saved to Response.OutputStream but leaves the rendered photo on the display. Covering the webpage. I do not want to see it just write them to a file for email attachments. Here is my code for the memory stream version excerts. But when I replace response.outputstream with mStream it displays and write a corrupt file.eg); – All_IN Feb 28 '20 at 17:12
  • Here is the memory stream excerpt. FileStream fileStream = new FileStream(sOrigBlankTicketImg, FileMode.Open); System.Drawing.Image bitmap = System.Drawing.Image.FromStream(fileStream); MemoryStream mStream = new MemoryStream(); bitmap.Save(mStream, ImageFormat.Jpeg); fileStream.Close();.. bitmap.Save(mStream, ImageFormat.Jpeg); string sSaveFile = Vars.TicketPath + "/" + sTicketNbr + ".jpg"; bitmap.Save(sSaveFile, System.Drawing.Imaging.ImageFormat.Jpg); – All_IN Feb 28 '20 at 17:13

0 Answers0