locked
Resize, re-sample and watermark image in THIS code! RRS feed

  • Question

  • User1173753381 posted

    Hi!

    I have a little bit of code that retrieves images from a database. I'd like to retrieve thumbnails from the large files in the database though.

     

    using System;
    using System.Collections;
    using System.Configuration;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;

    namespace image
    {
     public partial class _view : System.Web.UI.Page
     {
      private void Page_Load(object sender, System.EventArgs e)
      {
       string ImageId = Request.QueryString["img"];
       string sqlText = "SELECT img_data, img_contenttype FROM Image WHERE img_pk = " + ImageId;

       SqlConnection connection = new SqlConnection(ConfigurationSettings.AppSettings["DSN"].ToString());
       SqlCommand command = new SqlCommand(sqlText, connection);
       connection.Open();
       SqlDataReader dr = command.ExecuteReader();

       if (dr.Read())
       {
        Response.ContentType = dr["img_contenttype"].ToString();
        Response.BinaryWrite((byte[]) dr["img_data"]);
       }

       connection.Close();
      }
     }
    }

     

    That is the code, now where can I resize, re-sample and watermark this image? How would that code look?

     

    Thanks in advance, I'm sure some of you can help me! :-)

     Fredrik Sundström

    Monday, September 11, 2006 6:20 AM

All replies

  • User-1395392389 posted
     
    private void Page_Load(object sender, System.EventArgs e)
            {
                string ImageId = Request.QueryString["img"];
                string sqlText = "SELECT img_data, img_contenttype FROM Image WHERE img_pk = " + ImageId;
    
                SqlConnection connection = new SqlConnection(ConfigurationSettings.AppSettings["DSN"].ToString());
                SqlCommand command = new SqlCommand(sqlText, connection);
                connection.Open();
                SqlDataReader dr = command.ExecuteReader();
    
                if (dr.Read())
                {
                    Response.ContentType = dr["img_contenttype"].ToString();
                    byte[] bytes = (byte[])dr["img_data"];
                    System.IO.MemoryStream ms = new System.IO.MemoryStream(bytes);
                    Bitmap bmp = Bitmap.FromStream(ms);
                    ms.Close();
                    // <dummy code: insert your resizing logic here>
                    int width = 120;
                    int height = 90;
                    // </dummy code>
                    Size size = new Size(width, height);
                    Bitmap bmpThumb = new Bitmap(size.Width, size.Height);
                    Graphics grphThumb = Graphics.FromImage(bmpThumb);
                    grphThumb.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                    grphThumb.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                    grphThumb.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    grphThumb.DrawImage(bmp, 0, 0, size.Width, size.Height);
                    AddCopyRightTradeMark(bmpThumb, grphThumb);
                    ms = new System.IO.MemoryStream();
                    bmpThumb.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                    ms.WriteTo(Response.OutputStream);
                    ms.Close();
                }
                connection.Close();
    
            }
    
            private void AddCopyRightTradeMark(Bitmap bmp, Graphics graph)
            {
                StringFormat format = new StringFormat();
                format.Alignment = StringAlignment.Center;
                format.LineAlignment = StringAlignment.Near;
                RectangleF rect = new RectangleF(0f, (float)bmp.Height / 2f + 10f, (float)bmp.Width, 20f);
                RectangleF rect2 = new RectangleF(1f, (float)bmp.Height / 2f + 11f, (float)bmp.Width, 20f);
                SolidBrush brush = new SolidBrush(Color.White);
                SolidBrush brush2 = new SolidBrush(Color.Black);
                System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
                System.Drawing.Drawing2D.GraphicsPath path2 = new System.Drawing.Drawing2D.GraphicsPath();
                path.AddString("PACEM © [www.pacem.it]", new FontFamily("Arial Black"), 0, 10f, rect, format);
                path2.AddString("PACEM © [www.pacem.it]", new FontFamily("Arial Black"), 0, 10f, rect2, format);
                graph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                graph.FillPath(brush2, path2);
                graph.FillPath(brush, path);
            }
     Hope it could help.
    Tuesday, September 12, 2006 2:28 AM
  • User-1395392389 posted

    Sorry, I think I missed a cast (Image -> Bitmap):

    Bitmap bmp = (Bitmap)Bitmap.FromStream(ms);

    Tuesday, September 12, 2006 3:55 AM