Click here to Skip to main content
15,900,702 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a Image in Images Folder with 100 by 100 width & Height..Now I want this same image to resize to 640, 480 and display on another page in iframe without saving this new image anywhere in the folder..
Posted
Comments
Member 10435696 28-Mar-14 4:09am    
I have a ImageViewer Page With Iframe in it..And Server code like this-
frmShowImage.Attributes["src"] = "ImageViewerDownload.aspx?file=" + ContentData;

Then in ImageViewerDownload.aspx i have this code-

protected void Page_Load(object sender, EventArgs e)
{
try
{
Response.ContentType = "image/png";

var filePath = Server.MapPath("~/CmsImage/" + Request.QueryString["file"]);
System.Drawing.Image orignalImage = System.Drawing.Image.FromFile(filePath);
System.Drawing.Image resizedImage = FixedSize(orignalImage, 640, 480);

Response.WriteFile(Server.MapPath("~/CmsImage/" + resizedImage));
Response.Flush();
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
catch (Exception Ex)
{
LoggingBase.ErrorLog(CommonConstants.DISCOM_CODE, Ex.Message, MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name);
// mPopupMessage.ShowPopup("Application not Responding : please try later ", false);
}

}



internal static System.Drawing.Image FixedSize(System.Drawing.Image imgPhoto, int Width, int Height)
{
int sourceWidth = Convert.ToInt32(imgPhoto.Width);
int sourceHeight = Convert.ToInt32(imgPhoto.Height);
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;

float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;

nPercentW = ((float)Width / (float)sourceWidth);
nPercentH = ((float)Height / (float)sourceHeight);
if (nPercentH < nPercentW)
{
nPercent = nPercentH;
destX = System.Convert.ToInt16((Width -
(sourceWidth * nPercent)) / 2);
}
else
{
nPercent = nPercentW;
destY = System.Convert.ToInt16((Height -
(sourceHeight * nPercent)) / 2);
}

int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);

Bitmap bmPhoto = new Bitmap(Width, Height,
PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
imgPhoto.VerticalResolution);

Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.Clear(Color.Black);
grPhoto.InterpolationMode =
InterpolationMode.HighQualityBicubic;

grPhoto.DrawImage(imgPhoto,
new Rectangle(destX, destY, destWidth, destHeight),
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel);


grPhoto.Dispose();
return bmPhoto;
}


Now how to write a file which is not saved anywhere on folder.
Response.WriteFile(Server.MapPath("~/CmsImage/" + resizedImage));--->How to write this resized file in frame.Currently this line is giving error

1 solution

Try this link. I guess the image will be distorted when you increase the size.

http://www.microtuts.com/c-resize-an-image-proportionally-specify-max-widthheight/[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900