Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 348
  • Last Modified:

bitmap to place image within image (asp.net, c#)

I have started using Bitmap class to create images, from existing images.

Basically I have an image, which is nothing more than a background. I then create a Bitmap from the existing image, and use the DrawString to write something on this background. E.g. Company Name.

Now would it be possible to place on image (say for example a company logo) on this Bitmap? If yes, how would I go about it, what methods and arguments to use?

My code is as below... actually not mine, found it on the internet.
public Bitmap WaterMarkToImage(string ImagePath, string watermark)
    {
        Bitmap bmp;
        bmp = new Bitmap(ImagePath);
        Graphics graphicsObject;
        int x, y;
        try
        {
            //create graphics object from bitmap            
            graphicsObject = Graphics.FromImage(bmp);
        }
        catch (Exception e)
        {
            Bitmap bmpNew = new Bitmap(bmp.Width, bmp.Height);
            graphicsObject = Graphics.FromImage(bmpNew);
            graphicsObject.DrawImage(bmp, new Rectangle(0, 0, bmpNew.Width, bmpNew.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel);
            bmp = bmpNew;
        }
 
        int startsize = (bmp.Width / watermark.Length);
        //get the font size with respect to length of the string        
        //x and y cordinates to draw a string       
        x = 20;
        y = 80;//bmp.Height / 3;
        //System.Drawing.StringFormat drawFormat = new System.Drawing.StringFormat(StringFormatFlags.DirectionVertical); -> draws a vertical string for watermark        
        System.Drawing.StringFormat drawFormat = new System.Drawing.StringFormat(StringFormatFlags.NoWrap);
        //drawing string on Image        
        graphicsObject.DrawString(watermark, new Font("Verdana", startsize, FontStyle.Bold), new SolidBrush(Color.White), x, y, drawFormat);
        //new SolidBrush(Color.FromArgb(60, 255, 255, 255)) - water mark colour
        
        //return a water marked image       
        return (bmp);
    }
 
//end then calling it
 Watermark wM = new Watermark(); 
        System.Drawing.Bitmap bmp = wM.WaterMarkToImage("C:/base.jpg", txtTitle.Text);
        bmp.Save("C:/base2.jpg");

Open in new window

0
zeraxis_ee
Asked:
zeraxis_ee
  • 2
1 Solution
 
oobaylyCommented:
In line #16 of the example code it uses DrawImage. This is what you would use to draw the logo bitmap on the existing image, just as how you draw a string on the existing image
0
 
oobaylyCommented:
If in doubt, use the intellisense in VS to look through what methods & properties are available in an object (in this case the Graphics object).

The intellisense should also describe what the method does and an explanation of what each parameter is. Also the MSDN documentation is pretty good:
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawimage.aspx
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now