how to save a graphics into a jpg file

Now the graphics are painted in a form and i want to save the
graphics into a jpg file. Which class will implement that

what about ImageConverter class should i first transform the
graphics into a image. By the way, i am not sure whether the image
class will be useful.

thank you


Answer this question

how to save a graphics into a jpg file

  • Diego_VB

    Hi,
       did you ever find a solution for this

    I'm asking because I'm trying the exact same thing right now - with no luck.

    thanks,
    Ed.

  • kbickerton

    Daniel,
       should this stop my big black box error

    thanks,
    Ed.

  • vbn3wb1e



    Bitmap bmp = new Bitmap(700, 900);
    using (Graphics g = Graphics.FromImage(bmp))
    {
        g.DrawLine(Pens.Red, 0, 0, 100, 100);
    }
    bmp.Save(...);

     


  • Bud Pritchard

    Hi,
       I still get a big black box, here's some of the code I'm using:

    first my toolbar button for print:

    if (e.Button == this.tbPrintForm2) // print the validated version of the doc

    {
    _bmp = (Bitmap)_currFormImage;
    pd2 =
    new PrintDocument();
    pd2.PrintPage +=
    new PrintPageEventHandler(this.DrawPanel);
    PrintDialog myDialog =
    new PrintDialog();
    myDialog.Document = pd2;
    if(myDialog.ShowDialog() == DialogResult.OK)
    {
    pd2.Print();
    }
    }

    now the draw panel code - print event triggerred
    Note gr is being set here.

    private void DrawPanel(Object sender, PrintPageEventArgs e)
    {
    int resX = _bmp.Height;
    int resY= _bmp.Width;
    int leftOffsetVal = pd2.PrinterSettings.DefaultPageSettings.Margins.Left /5;
    e.Graphics.DrawImage(_bmp,0,0,
    new Rectangle(0,0,_bmp.Width, _bmp.Height), GraphicsUnit.Pixel);
    gr = e.Graphics;
    float scale = resX/resY;
    paintDynamicComponentsGraphics(leftOffsetVal,gr);
    }


    now of to the paint dynamic:
    tb is a text box.

    private void paintDynamicComponentsGraphics(int border, Graphics gr)
    {
    ...
    ..
    .
    .
    .
    .

    gr.DrawString(tb.Text, tb.Font, new SolidBrush(tb.ForeColor), (float)border + (float)field.LeftX, (float)border + (float)field.LeftY + ((float)field.Height-(float)gr.MeasureString("a", tb.Font).Height)/2.0f, new StringFormat());
    .
    .
    .
    .
    .

    Bitmap outputBitMap = new Bitmap(700,900);
    gr.DrawImage(outputBitMap,0,0,700,900);
    outputBitMap.Save("c:\\temp\\image1.jpg",ImageFormat.Jpeg);
    }

    the end

    the result is a jpg in the correct location but a big black rectangle - no background image or anything.

    thanks in advance for this, it's truly appreciated.
    Ed.


  • Shek Leung

    The image class has a Save method:

    image.Save("filename.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

  • Sergio Vasquez

    because I'm trying:

    Bitmap outputBitMap = new Bitmap(700,900,gr);
    outputBitMap.Save("c:\\temp\\image1.jpg",ImageFormat.Jpeg)

    and it's just giving me a big black rectangle.

    thanks again,
    Ed.


  • Vitali Lesheniuk

    Hi xudeutsch,
       forgive the seemingly boundless ineptitude but can you post an example of this.

    thanks in advance,
    Ed.

  • tdhers

    a graphics is painted in a bitmap object.
    and use Bitmap.save() method.


  • Rathna Kumar

    because i also get it out from a clip of another program, i am asking that person later.
    From your code, i think "gr"(stand for a graphics ) has no content, first you can paint the "gr"
    with the content you want to show in jpg,
    then "gr.DrawImage(outputBitMap);
                outputBitMap.Save("c:\\temp\\image1.jpg",ImageFormat.Jpeg;"

    i must to say i am trying to understand it further,  maybe someone else can explain us here better..

  • how to save a graphics into a jpg file