Reputation: 6849
I want to display image in specified size given in parameter like this https://i.sstatic.net/zOQ8r.jpg?s=128&g=1 .
Suppose my image1.jpg
is in image folder like this http://example.com/images/image1.jpg
then if I specify the parameter like ?s=200
then image should be resized to 200x200
size and display without any other element except <img>
tag.
I am not asking about code and example. I just need a logic or an idea. B'Coz, I am completely blank about it. Even I don't know what should I search on google. I am new in Web Application Development.
I want just same as http://lorempizza.com/ developed by rlemon
Plz give some ideas about it. Any help will be appreciated
Thanks & Regards
Upvotes: 3
Views: 733
Reputation: 6794
you can use the following function to re-size the image
public Image ImageResize(Image image, int width, int height)
{
Bitmap newImage = new Bitmap(width, height);
using (Graphics gr = Graphics.FromImage(newImage))
{
gr.SmoothingMode = SmoothingMode.AntiAlias;
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
gr.DrawImage(image, new Rectangle(0, 0, width, height));
}
return newImage as Image;
}
and to display the image
1- use the asp image tag
<asp:Image id="imageTag" runat="server"/>
2- save the Image to a folder by the following Image temp=ImageResize(YourImage,200,200);
temp.SaveAs(MapPath("~/Images/image1.png"));
3- map the image url to the path
imageTag.ImageUrl=Server.MapPath("~/Images/image1.png");
another approach, is to use the ashx handler if you insist to use <img>
, but at the end , asp:Image will be rendered as <img>
Edited Here
how to get the image resized using ashx
1- create a generic handler and name it ImageHandler
public class ImageHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
var directory = @"Location of the images" // example c:\Data;
var path = context.Request.QueryString["path"];
var size = Convert.ToInt32(context.Request.QueryString["size"]);
var image = Image.FromFile(string.Format(@"{0}\{1}.png", directory, path));
image = ImageResize(image, size, size);
context.Response.ContentType = "image/png";
context.Response.BinaryWrite(ImageToByte(image));
}
public Image ImageResize(Image image, int width, int height)
{
Bitmap newImage = new Bitmap(width, height);
using (Graphics gr = Graphics.FromImage(newImage))
{
gr.SmoothingMode = SmoothingMode.AntiAlias;
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
gr.DrawImage(image, new Rectangle(0, 0, width, height));
}
return newImage as Image;
}
public byte[] ImageToByte(Image img)
{
byte[] byteArray = new byte[0];
using (MemoryStream stream = new MemoryStream())
{
img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
stream.Close();
byteArray = stream.ToArray();
}
return byteArray;
}
public bool IsReusable
{
get
{
return false;
}
}
}
2- in the aspx page, use the following
<img src="~/ImageHandler.ashx?path=NameOfImage&size=SizeOfImage" runat="server" />
hope this will help you
regards
Upvotes: 3