Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Graph to image

Graph to image

Scheduled Pinned Locked Moved C#
graphicsquestiondatabasedata-structures
11 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Abel Castillo
    wrote on last edited by
    #1

    Hi, With images from files or from a database I don't have problems but when I work with images created starting from an object "Graphics" the images don't survive. How can I save this Bitmap?, I tried with: ... System.Drawing.Bitmap bmp = new System.Drawing.Bitmap( widthBmp, heightBmp ); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage( bmp ); ... MemoryStream stream = new MemoryStream(); bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg); byte[] buffer = new Byte[stream.Length]; stream.Position = 0; stream.Read(buffer, 0, (int) buffer.Length); ... buffer = null; stream.Close(); but the image always is black... Thanks a lot Abel Castillo

    M J 2 Replies Last reply
    0
    • A Abel Castillo

      Hi, With images from files or from a database I don't have problems but when I work with images created starting from an object "Graphics" the images don't survive. How can I save this Bitmap?, I tried with: ... System.Drawing.Bitmap bmp = new System.Drawing.Bitmap( widthBmp, heightBmp ); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage( bmp ); ... MemoryStream stream = new MemoryStream(); bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg); byte[] buffer = new Byte[stream.Length]; stream.Position = 0; stream.Read(buffer, 0, (int) buffer.Length); ... buffer = null; stream.Close(); but the image always is black... Thanks a lot Abel Castillo

      M Offline
      M Offline
      mikanu
      wrote on last edited by
      #2

      Hey there, I think there are a few problems in your code but I'm not 100% sure. First of all, your sample here doesn't really draw anyhting on the bitmap so - it should be black. Second of all, if you want to save the bitmap you will most likely need a FileStream, not a MemoryStream. The MemoryStream is just an in-memory stream. It will not be persisted anywhere. Maybe if you provide a bit more of your code here we'll be able to help out. Cheers ---- www.digitalGetto.com

      A 1 Reply Last reply
      0
      • A Abel Castillo

        Hi, With images from files or from a database I don't have problems but when I work with images created starting from an object "Graphics" the images don't survive. How can I save this Bitmap?, I tried with: ... System.Drawing.Bitmap bmp = new System.Drawing.Bitmap( widthBmp, heightBmp ); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage( bmp ); ... MemoryStream stream = new MemoryStream(); bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg); byte[] buffer = new Byte[stream.Length]; stream.Position = 0; stream.Read(buffer, 0, (int) buffer.Length); ... buffer = null; stream.Close(); but the image always is black... Thanks a lot Abel Castillo

        J Offline
        J Offline
        Josh Smith
        wrote on last edited by
        #3

        This works for me:

        private void button1_Click(object sender, System.EventArgs e)
        {
        using( Bitmap bmp = new Bitmap( 50, 50 ) )
        using( Graphics grfx = Graphics.FromImage( bmp ) )
        {
        grfx.FillRectangle( Brushes.Red, new Rectangle(0,0,50,50) );
        using( FileStream stream = new FileStream( @"C:\test.jpg", FileMode.Create ) )
        {
        bmp.Save( stream, System.Drawing.Imaging.ImageFormat.Jpeg );
        }
        }
        }

        I think the difference between our code is that I am saving to a FileStream, but you are saving to a MemoryStream. Your stream does not send the image to a file. Josh

        1 Reply Last reply
        0
        • M mikanu

          Hey there, I think there are a few problems in your code but I'm not 100% sure. First of all, your sample here doesn't really draw anyhting on the bitmap so - it should be black. Second of all, if you want to save the bitmap you will most likely need a FileStream, not a MemoryStream. The MemoryStream is just an in-memory stream. It will not be persisted anywhere. Maybe if you provide a bit more of your code here we'll be able to help out. Cheers ---- www.digitalGetto.com

          A Offline
          A Offline
          Abel Castillo
          wrote on last edited by
          #4

          The image is drawn in another method ("DrawGraphics") but I didn't include it because it is very long this it is a summary of the code: public System.Drawing.Bitmap CreateBitmap( ) { System.Drawing.Bitmap bmp = new System.Drawing.Bitmap( widthBmp, heightBmp ); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage( bmp ); DrawGraphics( g, new System.Drawing.Point( 0, 0 ) ); g.Dispose( ); return bmp; } System.Drawing.Bitmap bmp = CreateBitmap(); // make a memory stream to work with the image bytes MemoryStream stream = new MemoryStream(); // put the image into the memory stream bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg); // make byte array the same size as the image byte[] buffer = new Byte[stream.Length]; // rewind the memory stream stream.Position = 0; // load the byte array with the image stream.Read(buffer, 0, (int) buffer.Length); then save the image in sql database, but the array of bytes (variable buffer) has no images, it´s totally black... Thank you for the answer Abel Castillo

          M 1 Reply Last reply
          0
          • A Abel Castillo

            The image is drawn in another method ("DrawGraphics") but I didn't include it because it is very long this it is a summary of the code: public System.Drawing.Bitmap CreateBitmap( ) { System.Drawing.Bitmap bmp = new System.Drawing.Bitmap( widthBmp, heightBmp ); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage( bmp ); DrawGraphics( g, new System.Drawing.Point( 0, 0 ) ); g.Dispose( ); return bmp; } System.Drawing.Bitmap bmp = CreateBitmap(); // make a memory stream to work with the image bytes MemoryStream stream = new MemoryStream(); // put the image into the memory stream bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg); // make byte array the same size as the image byte[] buffer = new Byte[stream.Length]; // rewind the memory stream stream.Position = 0; // load the byte array with the image stream.Read(buffer, 0, (int) buffer.Length); then save the image in sql database, but the array of bytes (variable buffer) has no images, it´s totally black... Thank you for the answer Abel Castillo

            M Offline
            M Offline
            mikanu
            wrote on last edited by
            #5

            I'd say the problem is in the way you are creating the bitmap, and not how you save it into the MemoryStream. Josh is right, about not saving to a file but from your post I understand that it is not your intention to save to a file but rather to store the image bytes in a SQL database. I think the problem is in the CreateBitmap() function. I suspect it has to do with the fact that you create a graphics context from a bitmap that is empty. Than you draw to the graphics object but then dispose of it before saving. I'm not sure if that is what's going on but before you try saving it, draw it on a canvas on the screen to make sure the bitmap is properly created by CreateBitmap() ---- www.digitalGetto.com

            A 1 Reply Last reply
            0
            • M mikanu

              I'd say the problem is in the way you are creating the bitmap, and not how you save it into the MemoryStream. Josh is right, about not saving to a file but from your post I understand that it is not your intention to save to a file but rather to store the image bytes in a SQL database. I think the problem is in the CreateBitmap() function. I suspect it has to do with the fact that you create a graphics context from a bitmap that is empty. Than you draw to the graphics object but then dispose of it before saving. I'm not sure if that is what's going on but before you try saving it, draw it on a canvas on the screen to make sure the bitmap is properly created by CreateBitmap() ---- www.digitalGetto.com

              A Offline
              A Offline
              Abel Castillo
              wrote on last edited by
              #6

              I agree with you, I have no problems to save the image to a SQL database and to show it in a PictureBox. But when I tried to save in a array of bytes :( ... I'm working with the code of the excellent article Creating EAN-13 Barcodes with C#[^] by rainman_63. I generate the barcodes of several articles and I need to pass them to a database SQL, I have carried out several tests but up to now without success. Ean13 ean13 = new Ean13(); ean13.CountryCode = "84"; ean13.ManufacturerCode = "862"; ean13.ProductCode = "3274243"; ean13.ChecksumDigit = "7"; ean13.Scale = 1f; Bitmap bmp = ean13.CreateBitmap(); MemoryStream stream = new MemoryStream(); bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg); byte[] buffer = new Byte[stream.Length]; stream.Position = 0; stream.Read(buffer, 0, (int) buffer.Length); here save barcodes (buffer) in a SQL database buffer = null; stream.Close(); Thanks a lot Abel Castillo -- modified at 5:16 Thursday 22nd June, 2006

              A 1 Reply Last reply
              0
              • A Abel Castillo

                I agree with you, I have no problems to save the image to a SQL database and to show it in a PictureBox. But when I tried to save in a array of bytes :( ... I'm working with the code of the excellent article Creating EAN-13 Barcodes with C#[^] by rainman_63. I generate the barcodes of several articles and I need to pass them to a database SQL, I have carried out several tests but up to now without success. Ean13 ean13 = new Ean13(); ean13.CountryCode = "84"; ean13.ManufacturerCode = "862"; ean13.ProductCode = "3274243"; ean13.ChecksumDigit = "7"; ean13.Scale = 1f; Bitmap bmp = ean13.CreateBitmap(); MemoryStream stream = new MemoryStream(); bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg); byte[] buffer = new Byte[stream.Length]; stream.Position = 0; stream.Read(buffer, 0, (int) buffer.Length); here save barcodes (buffer) in a SQL database buffer = null; stream.Close(); Thanks a lot Abel Castillo -- modified at 5:16 Thursday 22nd June, 2006

                A Offline
                A Offline
                Abel Castillo
                wrote on last edited by
                #7

                link Creating EAN-13 Barcodes with C#[^] Abel Castillo

                M 1 Reply Last reply
                0
                • A Abel Castillo

                  link Creating EAN-13 Barcodes with C#[^] Abel Castillo

                  M Offline
                  M Offline
                  mikanu
                  wrote on last edited by
                  #8

                  I don't know what is not working in your application but here is code that I wrote to verify your concept and it all works. To me, the problem is in your CreateBitmap() function. To test the following code, create a new C# Windows Apllication. Drag and Drop two picture boxes on your form. Then paste this code as your Form Load event handler:

                          private void Form1_Load(object sender, EventArgs e)
                          {
                              MemoryStream memStream = new MemoryStream();
                              byte[] myBytes;
                  
                              using (Bitmap bmp = new Bitmap(50, 50))
                              using (Graphics grfx = Graphics.FromImage(bmp))
                              {
                                  grfx.FillRectangle(Brushes.Red, new Rectangle(0, 0, 50, 50));
                                  bmp.Save(memStream, System.Drawing.Imaging.ImageFormat.Bmp);
                                  myBytes = new byte[memStream.Length];
                                  memStream.Position = 0;
                                  memStream.Read(myBytes, 0, (int)memStream.Length);
                                  /* uncomment these lines to draw three white pixels on the second pictureBox */
                                  /*
                                  myBytes[101] = 255;
                                  myBytes[102] = 255;
                                  myBytes[103] = 255;
                                  myBytes[506] = 255;
                                  myBytes[507] = 255;
                                  myBytes[508] = 255;
                                  myBytes[910] = 255;
                                  myBytes[911] = 255;
                                  myBytes[912] = 255;
                                  */
                                  pictureBox1.Image = Image.FromStream(memStream);
                                  pictureBox2.Image = Image.FromStream(new MemoryStream(myBytes));
                              }
                          }
                  

                  *** Portion of the code above is taken from the post below[^] ---- www.digitalGetto.com -- modified at 12:38 Thursday 22nd June, 2006

                  A 1 Reply Last reply
                  0
                  • M mikanu

                    I don't know what is not working in your application but here is code that I wrote to verify your concept and it all works. To me, the problem is in your CreateBitmap() function. To test the following code, create a new C# Windows Apllication. Drag and Drop two picture boxes on your form. Then paste this code as your Form Load event handler:

                            private void Form1_Load(object sender, EventArgs e)
                            {
                                MemoryStream memStream = new MemoryStream();
                                byte[] myBytes;
                    
                                using (Bitmap bmp = new Bitmap(50, 50))
                                using (Graphics grfx = Graphics.FromImage(bmp))
                                {
                                    grfx.FillRectangle(Brushes.Red, new Rectangle(0, 0, 50, 50));
                                    bmp.Save(memStream, System.Drawing.Imaging.ImageFormat.Bmp);
                                    myBytes = new byte[memStream.Length];
                                    memStream.Position = 0;
                                    memStream.Read(myBytes, 0, (int)memStream.Length);
                                    /* uncomment these lines to draw three white pixels on the second pictureBox */
                                    /*
                                    myBytes[101] = 255;
                                    myBytes[102] = 255;
                                    myBytes[103] = 255;
                                    myBytes[506] = 255;
                                    myBytes[507] = 255;
                                    myBytes[508] = 255;
                                    myBytes[910] = 255;
                                    myBytes[911] = 255;
                                    myBytes[912] = 255;
                                    */
                                    pictureBox1.Image = Image.FromStream(memStream);
                                    pictureBox2.Image = Image.FromStream(new MemoryStream(myBytes));
                                }
                            }
                    

                    *** Portion of the code above is taken from the post below[^] ---- www.digitalGetto.com -- modified at 12:38 Thursday 22nd June, 2006

                    A Offline
                    A Offline
                    Abel Castillo
                    wrote on last edited by
                    #9

                    Hello mikanu, This is the code:

                    public System.Drawing.Bitmap CreateBitmap( )
                    {
                    float tempWidth = ( this.Width * this.Scale ) * 100 ;
                    float tempHeight = ( this.Height * this.Scale ) * 100;

                    System.Drawing.Bitmap bmp = new System.Drawing.Bitmap( (int)tempWidth, (int)tempHeight );

                    System.Drawing.Graphics g = System.Drawing.Graphics.FromImage( bmp );
                    this.DrawEan13Barcode( g, new System.Drawing.Point( 0, 0 ) );
                    g.Dispose( );
                    return bmp;
                    }

                    public void DrawEan13Barcode( System.Drawing.Graphics g, System.Drawing.Point pt )
                    {
                    float width = this.Width * this.Scale;
                    float height = this.Height * this.Scale;

                    // EAN13 Barcode should be a total of 113 modules wide.
                    float lineWidth = width / 113f;

                    // Save the GraphicsState.
                    System.Drawing.Drawing2D.GraphicsState gs = g.Save( );

                    // Set the PageUnit to Inch because all of our measurements are in inches.
                    g.PageUnit = System.Drawing.GraphicsUnit.Millimeter;

                    // Set the PageScale to 1, so a millimeter will represent a true millimeter.
                    g.PageScale = 1;

                    System.Drawing.SolidBrush brush = new System.Drawing.SolidBrush( System.Drawing.Color.Black );

                    float xPosition = 0;

                    System.Text.StringBuilder strbEAN13 = new System.Text.StringBuilder( );
                    System.Text.StringBuilder sbTemp = new System.Text.StringBuilder( );

                    float xStart = pt.X;
                    float yStart = pt.Y;
                    float xEnd = 0;

                    System.Drawing.Font font = new System.Drawing.Font( "Arial", this._fFontSize * this.Scale );

                    // Calculate the Check Digit.
                    this.CalculateChecksumDigit( );

                    sbTemp.AppendFormat( "{0}{1}{2}{3}",
                    this.CountryCode,
                    this.ManufacturerCode,
                    this.ProductCode,
                    this.ChecksumDigit );

                    string sTemp = sbTemp.ToString( );

                    string sLeftPattern = "";

                    // Convert the left hand numbers.
                    sLeftPattern = ConvertLeftPattern( sTemp.Substring( 0, 7 ) );

                    // Build the UPC Code.
                    strbEAN13.AppendFormat( "{0}{1}{2}{3}{4}{1}{0}",
                    this._sQuiteZone, this._sLeadTail,
                    sLeftPattern,
                    this._sSeparator,
                    ConvertToDigitPatterns( sTemp.Substring( 7 ), this._aRight ) );

                    string sTempUPC = strbEAN13.ToString( );

                    float fTextHeight = g.MeasureString( sTempUPC, font ).Height;

                    // Draw the barcode lines.
                    for( int i = 0; i < strbEAN13.Length; i++ )
                    {
                    if( sTempUPC.Substring( i, 1 ) == "1" )
                    {
                    if( xStart == pt.X )
                    xStart = xPosition;

                    // Save room for the UPC number below the bar code.
                    if( ( i > 12 && i < 55 ) || ( i > 57 && i < 101 ) )

                    M 1 Reply Last reply
                    0
                    • A Abel Castillo

                      Hello mikanu, This is the code:

                      public System.Drawing.Bitmap CreateBitmap( )
                      {
                      float tempWidth = ( this.Width * this.Scale ) * 100 ;
                      float tempHeight = ( this.Height * this.Scale ) * 100;

                      System.Drawing.Bitmap bmp = new System.Drawing.Bitmap( (int)tempWidth, (int)tempHeight );

                      System.Drawing.Graphics g = System.Drawing.Graphics.FromImage( bmp );
                      this.DrawEan13Barcode( g, new System.Drawing.Point( 0, 0 ) );
                      g.Dispose( );
                      return bmp;
                      }

                      public void DrawEan13Barcode( System.Drawing.Graphics g, System.Drawing.Point pt )
                      {
                      float width = this.Width * this.Scale;
                      float height = this.Height * this.Scale;

                      // EAN13 Barcode should be a total of 113 modules wide.
                      float lineWidth = width / 113f;

                      // Save the GraphicsState.
                      System.Drawing.Drawing2D.GraphicsState gs = g.Save( );

                      // Set the PageUnit to Inch because all of our measurements are in inches.
                      g.PageUnit = System.Drawing.GraphicsUnit.Millimeter;

                      // Set the PageScale to 1, so a millimeter will represent a true millimeter.
                      g.PageScale = 1;

                      System.Drawing.SolidBrush brush = new System.Drawing.SolidBrush( System.Drawing.Color.Black );

                      float xPosition = 0;

                      System.Text.StringBuilder strbEAN13 = new System.Text.StringBuilder( );
                      System.Text.StringBuilder sbTemp = new System.Text.StringBuilder( );

                      float xStart = pt.X;
                      float yStart = pt.Y;
                      float xEnd = 0;

                      System.Drawing.Font font = new System.Drawing.Font( "Arial", this._fFontSize * this.Scale );

                      // Calculate the Check Digit.
                      this.CalculateChecksumDigit( );

                      sbTemp.AppendFormat( "{0}{1}{2}{3}",
                      this.CountryCode,
                      this.ManufacturerCode,
                      this.ProductCode,
                      this.ChecksumDigit );

                      string sTemp = sbTemp.ToString( );

                      string sLeftPattern = "";

                      // Convert the left hand numbers.
                      sLeftPattern = ConvertLeftPattern( sTemp.Substring( 0, 7 ) );

                      // Build the UPC Code.
                      strbEAN13.AppendFormat( "{0}{1}{2}{3}{4}{1}{0}",
                      this._sQuiteZone, this._sLeadTail,
                      sLeftPattern,
                      this._sSeparator,
                      ConvertToDigitPatterns( sTemp.Substring( 7 ), this._aRight ) );

                      string sTempUPC = strbEAN13.ToString( );

                      float fTextHeight = g.MeasureString( sTempUPC, font ).Height;

                      // Draw the barcode lines.
                      for( int i = 0; i < strbEAN13.Length; i++ )
                      {
                      if( sTempUPC.Substring( i, 1 ) == "1" )
                      {
                      if( xStart == pt.X )
                      xStart = xPosition;

                      // Save room for the UPC number below the bar code.
                      if( ( i > 12 && i < 55 ) || ( i > 57 && i < 101 ) )

                      M Offline
                      M Offline
                      mikanu
                      wrote on last edited by
                      #10

                      The only difference that I could think of it that you dispose of the Graphics object in the CreateBitmap function whereas I created the MemoryStream before disposing the Graphics object. Give that a try.. I don't have a lot of time on my hands right now to test, but I'd be intereseted to know if that fixes it. Miky ---- www.digitalGetto.com

                      A 1 Reply Last reply
                      0
                      • M mikanu

                        The only difference that I could think of it that you dispose of the Graphics object in the CreateBitmap function whereas I created the MemoryStream before disposing the Graphics object. Give that a try.. I don't have a lot of time on my hands right now to test, but I'd be intereseted to know if that fixes it. Miky ---- www.digitalGetto.com

                        A Offline
                        A Offline
                        Abel Castillo
                        wrote on last edited by
                        #11

                        Thanks Miky, I'm making other tests and contacting with other programmers, when I find the solution I publish it. cheers Abel Abel Castillo

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups