Problem with Bitmaps and Graphics

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dataflashsabot
    New Member
    • Jan 2010
    • 4

    Problem with Bitmaps and Graphics

    I'm new to C#, so please forgive the ~407,000 coding sins I'm probably making.
    The purpose of this function is to take a bitmap and return a bitmap of double the height, with two of the images stacked on top of each other.



    Code:
    private Bitmap ProcessBitmap(Bitmap sourceBitmap)
            {
    
    
                int w = sourceBitmap.Width;
                int h = sourceBitmap.Height;
                int targetHeight = (h*2);
    
    
                
                Bitmap targetBitmap = new Bitmap(w, targetHeight);
    
                Rectangle sourceRect = new Rectangle(0, 0, w, h);
    
                Graphics sourceGraphics; 
                sourceGraphics = Graphics.FromImage(sourceBitmap);
                Graphics targetGraphics;
                targetGraphics = Graphics.FromImage(targetBitmap);
    
                // Begin implementation code
    
                targetGraphics.DrawImage(sourceBitmap, 0, 0);
                targetGraphics.DrawImage(sourceBitmap, 0, h);
    
                Bitmap returnBitmap = new Bitmap(w, targetHeight, targetGraphics);
                targetGraphics.Dispose();
    
    
    
                return returnBitmap;
            }
    But it seems to return an empty bitmap. Please help, I am at my wit's end. :-)
  • Dataflashsabot
    New Member
    • Jan 2010
    • 4

    #2
    Nevermind, I'm an idiot. The problem was in the code that was taking the return value. On the plus side, I rewrote the whole function trying to solve this, and it's now much neater and cleaner.

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      Maybe like this:
      [code=C#]
      private Bitmap ProcessBitmap(B itmap sourceBitmap)
      {
      int h = sourceBitmap.He ight;
      Bitmap returnBitmap = new Bitmap(sourceBi tmap.Width, h*2);

      Graphics targetGraphics = Graphics.FromIm age(targetBitma p);

      targetGraphics. DrawImageUnscal ed(sourceBitmap , 0, 0);
      targetGraphics. DrawImageUnscal ed(sourceBitmap , 0, h);

      return returnBitmap;
      }
      [/code]

      Comment

      • akshaymahajan
        New Member
        • Sep 2009
        • 12

        #4
        Please forgive me if I sound rude ... but u don't need int h in your code. Just replace h by sourceBitmap.He ight ... Just trying to save some memory space for the application :) .. Hope u don't mind.

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          You could collapse the code a lot more then just getting rid of the h. Its just there so you can follow along

          Comment

          Working...