Graphics

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • DaveL

    #1

    Graphics

    hello
    I have a Bit map 1367 wide 32 high
    this bitmap contains like 40 separate Images
    32x32
    I tell it the id*32 to get the approiate Image from
    the source Bitmap


    When i CreateGraphics( ) From the Standard CreateGraphics( ) function the code
    below works
    perfect..
    when i CreateGraphics. FromImage()
    the Resuting Bitmap is Not Copied Right.... i think. when i assign the
    bitmap to a picturebox or button its not correct


    can anybody tell me what the problem is here

    int Id=10;
    Rectangle Rect = new Rectangle(new Point(Id*32, 0), new Size(32,
    32));
    Bitmap bmp = new Bitmap(Rect.Wid th, Rect.Height);

    //Graphics g = Graphics.FromIm age(bmp); // draws to
    bitmap but is all messed up
    Graphics g = CreateGraphics( ); //
    works perfect with this but draws to the window at 0,0 i want a bitmap back
    g.DrawImage(Sou rce, 0, 0, Rect, GraphicsUnit.Pi xel);
    g.Dispose();


    Thanks DaveL


  • Jeff Johnson

    #2
    Re: Graphics

    "DaveL" <dvs_bis@sbcglo bal.netwrote in message
    news:6E1Mk.6091 $Ws1.2712@nlpi0 64.nbdc.sbc.com ...
    hello
    I have a Bit map 1367 wide 32 high
    this bitmap contains like 40 separate Images
    32x32
    I tell it the id*32 to get the approiate Image from
    the source Bitmap
    >
    >
    When i CreateGraphics( ) From the Standard CreateGraphics( ) function the
    code below works
    perfect..
    when i CreateGraphics. FromImage()
    the Resuting Bitmap is Not Copied Right.... i think. when i assign the
    bitmap to a picturebox or button its not correct
    >
    >
    can anybody tell me what the problem is here
    >
    int Id=10;
    Rectangle Rect = new Rectangle(new Point(Id*32, 0), new Size(32,
    32));
    Bitmap bmp = new Bitmap(Rect.Wid th, Rect.Height);
    >
    //Graphics g = Graphics.FromIm age(bmp); // draws to
    bitmap but is all messed up
    Graphics g = CreateGraphics( ); //
    works perfect with this but draws to the window at 0,0 i want a bitmap
    back
    g.DrawImage(Sou rce, 0, 0, Rect, GraphicsUnit.Pi xel);
    g.Dispose();
    You might want to repost in microsoft.publi c.dotnet.framew ork.drawing.
    However, where in that posted code are you using the bmp variable? The only
    place I see it is in the stuff you commented out. And where do you want to
    draw the bitmap to? You'll need two Graphics objects: one for the bitmap
    (the source) and one for the destination.


    Comment

    • DaveL

      #3
      Re: Graphics

      New bmp is created 32x32
      CreateGraphics( ) draws to the form perfect
      when i CreateGraphics. FromImage(bmp) // new bmp
      then i use g.drawImage(Sou rce < source bmp, 0,0,Rect,Graphi csUnit.Pixel);
      // draws to new bmp but when i place bmp on control not correct


      that is all below

      "Jeff Johnson" <i.get@enough.s pamwrote in message
      news:OTDnK6TNJH A.276@TK2MSFTNG P02.phx.gbl...
      "DaveL" <dvs_bis@sbcglo bal.netwrote in message
      news:6E1Mk.6091 $Ws1.2712@nlpi0 64.nbdc.sbc.com ...
      >hello
      >I have a Bit map 1367 wide 32 high
      >this bitmap contains like 40 separate Images
      >32x32
      >I tell it the id*32 to get the approiate Image from
      >the source Bitmap
      >>
      >>
      >When i CreateGraphics( ) From the Standard CreateGraphics( ) function the
      >code below works
      >perfect..
      >when i CreateGraphics. FromImage()
      >the Resuting Bitmap is Not Copied Right.... i think. when i assign the
      >bitmap to a picturebox or button its not correct
      >>
      >>
      >can anybody tell me what the problem is here
      >>
      > int Id=10;
      > Rectangle Rect = new Rectangle(new Point(Id*32, 0), new
      >Size(32, 32));
      > Bitmap bmp = new Bitmap(Rect.Wid th, Rect.Height);
      >>
      > //Graphics g = Graphics.FromIm age(bmp); // draws to
      >bitmap but is all messed up
      > Graphics g = CreateGraphics( ); //
      >works perfect with this but draws to the window at 0,0 i want a bitmap
      >back
      > g.DrawImage(Sou rce, 0, 0, Rect, GraphicsUnit.Pi xel);
      > g.Dispose();
      >
      You might want to repost in microsoft.publi c.dotnet.framew ork.drawing.
      However, where in that posted code are you using the bmp variable? The
      only place I see it is in the stuff you commented out. And where do you
      want to draw the bitmap to? You'll need two Graphics objects: one for the
      bitmap (the source) and one for the destination.
      >

      Comment

      • Lee

        #4
        Re: Graphics

        This function returns a bitmap that is a sub-section of the original

        If you look at your code and this function you will see where your
        thought process went awry.

        Remember that if you create a graphics object from a bitmap, then
        anything done to the graphics object is applied to your bitmap, so
        after your g.DrawImage, all you needed to do was use your altered
        bitmap.

        I created a dummy 1367x32 image then an altered version of your code,
        I itterated through id from 0 to 39 and set a picturebox image
        property to the resulting bitmap. All looked fine.

        The function below was found at: http://msdn.microsoft.com/en-us/library/aa457087.aspx

        public Bitmap Copy(Bitmap srcBitmap, Rectangle section)
        {
        // Create the new bitmap and associated graphics object
        Bitmap bmp = new Bitmap(section. Width, section.Height) ;
        Graphics g = Graphics.FromIm age(bmp);

        // Draw the specified section of the source bitmap to the
        new one
        g.DrawImage(src Bitmap, 0, 0, section, GraphicsUnit.Pi xel);

        // Clean up
        g.Dispose();

        // Return the bitmap
        return bmp;
        }
        }

        L. Lee Saunders
        Playing with old ideas/concepts using the newest tools!


        On Oct 23, 11:30 am, "DaveL" <dvs_...@sbcglo bal.netwrote:
        hello
        I have a Bit map 1367 wide 32 high
        this bitmap contains like 40 separate Images
        32x32
        I tell it the id*32 to get the approiate Image from
        the source Bitmap
        >
        When i CreateGraphics( ) From the Standard CreateGraphics( ) function the code
        below works
        perfect..
        when i CreateGraphics. FromImage()
        the Resuting Bitmap is Not Copied Right.... i think. when i assign the
        bitmap to a picturebox or button its not correct
        >
        can anybody tell me what the problem is here
        >
                   int Id=10;
                   Rectangle Rect = new Rectangle(new Point(Id*32, 0), new Size(32,
        32));
                   Bitmap bmp = new Bitmap(Rect.Wid th, Rect.Height);
        >
                   //Graphics g = Graphics.FromIm age(bmp);           // draws to
        bitmap but is all messed up
                   Graphics g = CreateGraphics( );                             //
        works perfect with this but draws to the window at 0,0 i want a bitmap back
                   g.DrawImage(Sou rce, 0, 0, Rect, GraphicsUnit.Pi xel);
                   g.Dispose();
        >
        Thanks DaveL

        Comment

        • DaveL

          #5
          Re: Graphics

          Lee, I am using my alterd bitmap
          below is new code from that link you posted
          i still get the same results, if i draw to screen perfect
          if i draw to bitmap not perfect


          private void button3_Click(o bject sender, EventArgs e)
          {
          Bitmap srcBitmap =
          (Bitmap)Bitmap. FromFile(@"D:\_ netdev\dev\pics \band32.bmp");
          Rectangle section = new Rectangle(16 * 32, 0, 32, 32);
          // Create the new bitmap and associated graphics object
          Bitmap bmp = new Bitmap(section. Width, section.Height) ;
          Graphics g = Graphics.FromIm age(bmp);

          // Draw the specified section of the source bitmap to the new
          one
          g.DrawImage(src Bitmap, 0, 0, section, GraphicsUnit.Pi xel);

          // Clean up
          g.Dispose();
          this.button3.Im age = bmp;

          "Lee" <saunderl@hotma il.comwrote in message
          news:6fd25075-3214-4522-a6e0-50d026eda5b6@m3 g2000hsc.google groups.com...
          This function returns a bitmap that is a sub-section of the original

          If you look at your code and this function you will see where your
          thought process went awry.

          Remember that if you create a graphics object from a bitmap, then
          anything done to the graphics object is applied to your bitmap, so
          after your g.DrawImage, all you needed to do was use your altered
          bitmap.

          I created a dummy 1367x32 image then an altered version of your code,
          I itterated through id from 0 to 39 and set a picturebox image
          property to the resulting bitmap. All looked fine.

          The function below was found at:
          http://msdn.microsoft.com/en-us/library/aa457087.aspx

          public Bitmap Copy(Bitmap srcBitmap, Rectangle section)
          {
          // Create the new bitmap and associated graphics object
          Bitmap bmp = new Bitmap(section. Width, section.Height) ;
          Graphics g = Graphics.FromIm age(bmp);

          // Draw the specified section of the source bitmap to the
          new one
          g.DrawImage(src Bitmap, 0, 0, section, GraphicsUnit.Pi xel);

          // Clean up
          g.Dispose();

          // Return the bitmap
          return bmp;
          }
          }

          L. Lee Saunders
          Playing with old ideas/concepts using the newest tools!


          On Oct 23, 11:30 am, "DaveL" <dvs_...@sbcglo bal.netwrote:
          hello
          I have a Bit map 1367 wide 32 high
          this bitmap contains like 40 separate Images
          32x32
          I tell it the id*32 to get the approiate Image from
          the source Bitmap
          >
          When i CreateGraphics( ) From the Standard CreateGraphics( ) function the
          code
          below works
          perfect..
          when i CreateGraphics. FromImage()
          the Resuting Bitmap is Not Copied Right.... i think. when i assign the
          bitmap to a picturebox or button its not correct
          >
          can anybody tell me what the problem is here
          >
          int Id=10;
          Rectangle Rect = new Rectangle(new Point(Id*32, 0), new Size(32,
          32));
          Bitmap bmp = new Bitmap(Rect.Wid th, Rect.Height);
          >
          //Graphics g = Graphics.FromIm age(bmp); // draws to
          bitmap but is all messed up
          Graphics g = CreateGraphics( ); //
          works perfect with this but draws to the window at 0,0 i want a bitmap
          back
          g.DrawImage(Sou rce, 0, 0, Rect, GraphicsUnit.Pi xel);
          g.Dispose();
          >
          Thanks DaveL

          Comment

          • Peter Duniho

            #6
            Re: Graphics

            On Thu, 23 Oct 2008 09:30:13 -0700, DaveL <dvs_bis@sbcglo bal.netwrote:
            hello
            I have a Bit map 1367 wide 32 high
            this bitmap contains like 40 separate Images
            32x32
            I tell it the id*32 to get the approiate Image from
            the source Bitmap
            Note that 1367 isn't an even multiple of 32, never mind is it 40 * 32.

            As far as your actual question goes, the code you posted looks fine so far
            as it goes. But, it's not a concise-but-complete code sample, so there's
            no way to know exactly what's going wrong. For sure, the general
            technique you're describing works fine, when you've done everything right.

            I've attached below a (mostly) concise-but-complete code sample that
            demonstrates the technique you're trying to accomplish. My code works
            fine. If you want help with your code, you need to post a
            concise-but-complete code sample that _doesn't_ work fine.

            Note: I say "mostly", because I left out the Designer-provided stuff; it's
            a little more concise at the expense of being truly complete. In this
            case, I think that's fine because I didn't change anything at all with
            respect to the defaults. You can create a blank Forms application and
            just copy-and-paste my code into the Form1.cs file and it will work. You
            should be able to provide a similar concise-but-complete code sample.

            Pete


            using System;
            using System.Drawing;
            using System.Windows. Forms;

            namespace TestDrawToBitma p
            {
            public partial class Form1 : Form
            {
            private const int _kiimgMax = 40;

            private Image _imgSource;
            private Image _imgCurrent;
            private Random _rnd = new Random();

            public Form1()
            {
            InitializeCompo nent();

            _imgSource = _ImageInitSourc e();
            _imgCurrent = _ImageNextRando m();

            Timer timer = new Timer();

            timer.Tick += _TickHandler;
            timer.Interval = 1000;
            timer.Start();
            }

            protected override void OnFormClosed(Fo rmClosedEventAr gs e)
            {
            _imgCurrent.Dis pose();
            _imgSource.Disp ose();

            base.OnFormClos ed(e);
            }

            protected override void OnPaint(PaintEv entArgs e)
            {
            base.OnPaint(e) ;

            e.Graphics.Draw Image(_imgCurre nt, (ClientRectangl e.Width -
            _imgCurrent.Wid th) / 2, (ClientRectangl e.Height - _imgCurrent.Hei ght) / 2);
            }

            protected override void OnResize(EventA rgs e)
            {
            base.OnResize(e );

            Invalidate();
            }

            private void _TickHandler(ob ject sender, EventArgs e)
            {
            _imgCurrent.Dis pose();
            _imgCurrent = _ImageNextRando m();
            Invalidate();
            }

            private Image _ImageNextRando m()
            {
            int cxImage = _imgSource.Widt h / _kiimgMax;
            Point ptSource = new Point(_rnd.Next (_kiimgMax) * cxImage, 0);
            Image imgRet = new Bitmap(cxImage, _imgSource.Heig ht);

            using (Graphics gfx = Graphics.FromIm age(imgRet))
            {
            gfx.DrawImage(_ imgSource, 0, 0, new Rectangle(ptSou rce,
            imgRet.Size), GraphicsUnit.Pi xel);
            }

            return imgRet;
            }

            private Image _ImageInitSourc e()
            {
            SizeF sizeText;

            using (Image img = new Bitmap(1, 1))
            using (Graphics gfx = Graphics.FromIm age(img))
            {
            sizeText = gfx.MeasureStri ng("00", Font);
            }

            int cxText = (int)(sizeText. Width + 0.5f),
            cyText = (int)(sizeText. Height + 0.5f);
            Image imgRet = new Bitmap(_kiimgMa x * cxText, cyText);
            StringFormat format = StringFormat.Ge nericDefault;

            format.Alignmen t = StringAlignment .Center;
            format.LineAlig nment = StringAlignment .Center;
            format.Trimming = StringTrimming. None;
            format.FormatFl ags |= StringFormatFla gs.NoWrap;

            using (Brush brushForeground = new SolidBrush(Fore Color))
            using (Graphics gfx = Graphics.FromIm age(imgRet))
            {
            gfx.Clear(BackC olor);

            for (int i = 0; i < _kiimgMax; i++)
            {
            string strText = i.ToString("00" );
            RectangleF rectLayout = new RectangleF(new PointF(i *
            cxText, 0), new SizeF(cxText, cyText));

            gfx.DrawString( strText, Font, brushForeground ,
            rectLayout, format);
            }
            }

            return imgRet;
            }
            }
            }

            Comment

            • Jeff Johnson

              #7
              Re: Graphics

              "DaveL" <dvs_bis@sbcglo bal.netwrote in message
              news:Qf4Mk.4440 $ZP4.3485@nlpi0 67.nbdc.sbc.com ...
              Lee, I am using my alterd bitmap
              below is new code from that link you posted
              i still get the same results, if i draw to screen perfect
              if i draw to bitmap not perfect
              >
              >
              private void button3_Click(o bject sender, EventArgs e)
              {
              Bitmap srcBitmap =
              (Bitmap)Bitmap. FromFile(@"D:\_ netdev\dev\pics \band32.bmp");
              Rectangle section = new Rectangle(16 * 32, 0, 32, 32);
              // Create the new bitmap and associated graphics object
              Bitmap bmp = new Bitmap(section. Width, section.Height) ;
              Graphics g = Graphics.FromIm age(bmp);
              >
              // Draw the specified section of the source bitmap to the new
              one
              g.DrawImage(src Bitmap, 0, 0, section, GraphicsUnit.Pi xel);
              >
              // Clean up
              g.Dispose();
              this.button3.Im age = bmp;
              You probably need to set the PixelFormat of the Bitmap to the proper value.
              You may be losing resolution.

              But so far you've said "it's wrong" and it's "not perfect." This is not
              giving us much information on what's REALLY wrong.


              Comment

              • Lee

                #8
                Re: Graphics

                Your image is in a lower DPI (76) than screen dpi (96). It is an
                issue with Bitmaps. If you saved it as a PNG, it would look fine.
                Otherwise, sections to the right and bottom are getting cut off.

                I changed the DPI of the image to 96 and now it works just fine.

                L. Lee Saunders
                Playing with old ideas/concepts using the newest tools!

                Comment

                • DaveL

                  #9
                  Re: Graphics

                  when graphics draws to the screen
                  its correct 32x32 same as in the original bitmap
                  just the 32x32 portion

                  when i use graphics created from the new bitmap
                  i get somewhat the correct area of the original bitmap
                  but its not correct
                  I really dont know who to explain this problem

                  one way works fine , the other does not work correctly

                  Dave

                  "Jeff Johnson" <i.get@enough.s pamwrote in message
                  news:uyQgDmUNJH A.5648@TK2MSFTN GP05.phx.gbl...
                  "DaveL" <dvs_bis@sbcglo bal.netwrote in message
                  news:Qf4Mk.4440 $ZP4.3485@nlpi0 67.nbdc.sbc.com ...
                  >
                  >Lee, I am using my alterd bitmap
                  >below is new code from that link you posted
                  >i still get the same results, if i draw to screen perfect
                  >if i draw to bitmap not perfect
                  >>
                  >>
                  >private void button3_Click(o bject sender, EventArgs e)
                  > {
                  > Bitmap srcBitmap =
                  >(Bitmap)Bitmap .FromFile(@"D:\ _netdev\dev\pic s\band32.bmp");
                  > Rectangle section = new Rectangle(16 * 32, 0, 32, 32);
                  > // Create the new bitmap and associated graphics object
                  > Bitmap bmp = new Bitmap(section. Width, section.Height) ;
                  > Graphics g = Graphics.FromIm age(bmp);
                  >>
                  > // Draw the specified section of the source bitmap to the new
                  >one
                  > g.DrawImage(src Bitmap, 0, 0, section, GraphicsUnit.Pi xel);
                  >>
                  > // Clean up
                  > g.Dispose();
                  > this.button3.Im age = bmp;
                  >
                  You probably need to set the PixelFormat of the Bitmap to the proper
                  value. You may be losing resolution.
                  >
                  But so far you've said "it's wrong" and it's "not perfect." This is not
                  giving us much information on what's REALLY wrong.
                  >

                  Comment

                  • DaveL

                    #10
                    Re: Graphics

                    Lee
                    I was wondering about that,
                    I did look into the resolution, but did
                    not know how to change it

                    Just like to thank you very much
                    for your time...

                    DaveP

                    "Lee" <saunderl@hotma il.comwrote in message
                    news:a44dfd27-d352-44b3-ba7a-dd4710e9d527@k1 6g2000hsf.googl egroups.com...
                    Your image is in a lower DPI (76) than screen dpi (96). It is an
                    issue with Bitmaps. If you saved it as a PNG, it would look fine.
                    Otherwise, sections to the right and bottom are getting cut off.
                    >
                    I changed the DPI of the image to 96 and now it works just fine.
                    >
                    L. Lee Saunders
                    http://oldschooldotnet.blogspot.com

                    Comment

                    Working...