locked
creating thumbnails RRS feed

  • Question

  • User-1781885172 posted

    i am creating a photo gallery, in which i want to add thumbnails, and my page is running properly, but the thumbnail's size is large, i want to reduce size of thumbnails.

    here is my code:

    protected void Page_Load(object sender, EventArgs e)
    {

    try
    {
    using (SqlConnection conect = new SqlConnection(con))
    {
    conect.Open();
    SqlCommand command = conect.CreateCommand();
    command.Connection = conect;

    command.Parameters.Clear();
    command.Parameters.AddWithValue("fid", id);

    command.CommandText = "SELECT TOP 1 file_data, file_mime_type FROM [File_Upload_Media] WHERE file_id=@fid";   

    SqlDataReader myreader = command.ExecuteReader();
    if (myreader.HasRows)
    {
    myreader.Read();
    b = ((byte[])myreader["file_data"]);

    Response.ContentType = myreader["file_mime_type"].ToString();
    Response.BinaryWrite(b);
    myreader.Close();
    }

    }

    }

    catch

    {

    }

    }

    Thursday, January 10, 2013 5:19 AM

Answers

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 10, 2013 5:21 AM
  • User-422529730 posted

    use below function

    public static byte[] ResizeImage(byte[] bytes, int NewWidth, int MaxHeight, bool AllowLargerImageCreation)
    {
    
    	Image FullsizeImage = null;
    	Image ResizedImage = null;
    	//Cast bytes to an image
    	FullsizeImage = byteArrayToImage(bytes);
    
    	// Prevent using images internal thumbnail
    	FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
    	FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
    
    	// If we are re sizing upwards to a bigger size
    	if (AllowLargerImageCreation) {
    		if (FullsizeImage.Width <= NewWidth) {
    			NewWidth = FullsizeImage.Width;
    		}
    	}
    
    	//Keep aspect ratio
    	int NewHeight = FullsizeImage.Height * NewWidth / FullsizeImage.Width;
    	if (NewHeight > MaxHeight) {
    		// Resize with height instead
    		NewWidth = FullsizeImage.Width * MaxHeight / FullsizeImage.Height;
    		NewHeight = MaxHeight;
    	}
    
    	ResizedImage = FullsizeImage.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero);
    
    	// Clear handle to original file so that we can overwrite it if necessary
    	FullsizeImage.Dispose();
    
    	return imageToByteArray(ResizedImage);
    }
    
    
    /// <summary>
    /// convert image to byte array
    /// </summary>
    /// <param name="imageIn"></param>
    /// <returns></returns>
    private static byte[] imageToByteArray(System.Drawing.Image imageIn)
    {
    	MemoryStream ms = new MemoryStream();
    	imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
    	return ms.ToArray();
    }
    
    
    /// <summary>
    /// Convert a byte array to an image
    /// </summary>
    /// <remarks></remarks>
    public static Image byteArrayToImage(byte[] byteArrayIn)
    {
    	MemoryStream ms = new MemoryStream(byteArrayIn);
    	Image returnImage = Image.FromStream(ms);
    	return returnImage;
    }

    update your code

    protected void Page_Load(object sender, EventArgs e)
    {
    
    try
    {
    using (SqlConnection conect = new SqlConnection(con))
    {
    conect.Open();
    SqlCommand command = conect.CreateCommand();
    command.Connection = conect;
    
    command.Parameters.Clear();
    command.Parameters.AddWithValue("fid", id);
    
    command.CommandText = "SELECT TOP 1 file_data, file_mime_type FROM [File_Upload_Media] WHERE file_id=@fid";   
    
    SqlDataReader myreader = command.ExecuteReader();
    if (myreader.HasRows)
    {
    myreader.Read();
    b = ((byte[])myreader["file_data"]);
    
    Response.ContentType = myreader["file_mime_type"].ToString();
    Response.BinaryWrite(ResizeImage(b, 50, 50, True));
    myreader.Close();
    }
    
    }
    
    }
    
    catch
    
    {
    
    }
    
    }




    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 10, 2013 5:30 AM

All replies

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 10, 2013 5:21 AM
  • User-422529730 posted

    use below function

    public static byte[] ResizeImage(byte[] bytes, int NewWidth, int MaxHeight, bool AllowLargerImageCreation)
    {
    
    	Image FullsizeImage = null;
    	Image ResizedImage = null;
    	//Cast bytes to an image
    	FullsizeImage = byteArrayToImage(bytes);
    
    	// Prevent using images internal thumbnail
    	FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
    	FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
    
    	// If we are re sizing upwards to a bigger size
    	if (AllowLargerImageCreation) {
    		if (FullsizeImage.Width <= NewWidth) {
    			NewWidth = FullsizeImage.Width;
    		}
    	}
    
    	//Keep aspect ratio
    	int NewHeight = FullsizeImage.Height * NewWidth / FullsizeImage.Width;
    	if (NewHeight > MaxHeight) {
    		// Resize with height instead
    		NewWidth = FullsizeImage.Width * MaxHeight / FullsizeImage.Height;
    		NewHeight = MaxHeight;
    	}
    
    	ResizedImage = FullsizeImage.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero);
    
    	// Clear handle to original file so that we can overwrite it if necessary
    	FullsizeImage.Dispose();
    
    	return imageToByteArray(ResizedImage);
    }
    
    
    /// <summary>
    /// convert image to byte array
    /// </summary>
    /// <param name="imageIn"></param>
    /// <returns></returns>
    private static byte[] imageToByteArray(System.Drawing.Image imageIn)
    {
    	MemoryStream ms = new MemoryStream();
    	imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
    	return ms.ToArray();
    }
    
    
    /// <summary>
    /// Convert a byte array to an image
    /// </summary>
    /// <remarks></remarks>
    public static Image byteArrayToImage(byte[] byteArrayIn)
    {
    	MemoryStream ms = new MemoryStream(byteArrayIn);
    	Image returnImage = Image.FromStream(ms);
    	return returnImage;
    }

    update your code

    protected void Page_Load(object sender, EventArgs e)
    {
    
    try
    {
    using (SqlConnection conect = new SqlConnection(con))
    {
    conect.Open();
    SqlCommand command = conect.CreateCommand();
    command.Connection = conect;
    
    command.Parameters.Clear();
    command.Parameters.AddWithValue("fid", id);
    
    command.CommandText = "SELECT TOP 1 file_data, file_mime_type FROM [File_Upload_Media] WHERE file_id=@fid";   
    
    SqlDataReader myreader = command.ExecuteReader();
    if (myreader.HasRows)
    {
    myreader.Read();
    b = ((byte[])myreader["file_data"]);
    
    Response.ContentType = myreader["file_mime_type"].ToString();
    Response.BinaryWrite(ResizeImage(b, 50, 50, True));
    myreader.Close();
    }
    
    }
    
    }
    
    catch
    
    {
    
    }
    
    }




    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 10, 2013 5:30 AM
  • User1139353921 posted

    http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/e2d8bd99-1af4-404a-a6bd-5fae9f540c2d/

    Thursday, January 10, 2013 5:57 AM