Answered by:
Resizing the images by preserving the clarity

Question
-
I have to re size an image pro grammatically with out loss of clarity.
I have an image file of one or more multiple images in it. Also they will have text in it.So when the images is resized , inner images should have the same clarity, text should be readable. and the re sized image size should be with in the defined size.
Please help me
Regards,
Swetha
Thursday, June 2, 2011 5:41 AM
Answers
-
Try below link
http://www.uncommentedcode.com/post/2009/03/27/Great-way-to-resize-images-in-C.aspx
Thanks,
A.m.a.L Hashim
Dot Net Goodies
Don't hate the hacker, hate the code - Marked as answer by Aspen VJ Monday, June 13, 2011 7:40 AM
Thursday, June 2, 2011 6:03 AM
All replies
-
I have to re size an image pro grammatically with out loss of clarity.
I have an image file of one or more multiple images in it. Also they will have text in it.So when the images is resized , inner images should have the same clarity, text should be readable. and the re sized image size should be with in the defined size.
Please help me
Regards,
Swetha
- Merged by Aspen VJ Friday, June 3, 2011 6:09 AM duplicated
Thursday, June 2, 2011 5:49 AM -
I have to re size an image pro grammatically with out loss of clarity.
I have an image file of one or more multiple images in it. Also they will have text in it.So when the images is resized , inner images should have the same clarity, text should be readable. and the re sized image size should be with in the defined size.
Please help me
Regards,
Swetha
- Changed type SwethaSyam Thursday, June 2, 2011 6:58 AM
- Merged by Aspen VJ Friday, June 3, 2011 6:09 AM duplicated
Thursday, June 2, 2011 5:49 AM -
Below 2 urls might help, having sample code
http://stackoverflow.com/questions/87753/resizing-an-image-without-losing-any-quality
Malayalam SMS Website http://aspspider.info/smsmalluThursday, June 2, 2011 5:53 AM -
Hi Swetha,
Since this is a question, please change type to Question instead of Discussion.
And don't post multiple times.
Thanks,
A.m.a.L Hashim
Dot Net Goodies
Don't hate the hacker, hate the code Thursday, June 2, 2011 6:02 AM -
Try below link
http://www.uncommentedcode.com/post/2009/03/27/Great-way-to-resize-images-in-C.aspx
Thanks,
A.m.a.L Hashim
Dot Net Goodies
Don't hate the hacker, hate the code - Marked as answer by Aspen VJ Monday, June 13, 2011 7:40 AM
Thursday, June 2, 2011 6:03 AM -
Try below link
http://www.uncommentedcode.com/post/2009/03/27/Great-way-to-resize-images-in-C.aspx
Thanks,
A.m.a.L Hashim
Dot Net Goodies
Don't hate the hacker, hate the code - Proposed as answer by Aspen VJ Friday, June 3, 2011 6:04 AM
Thursday, June 2, 2011 6:04 AM -
Hi Shweta,
Are you talking about setting a Watermark image on the main image file that we usually do in case of putting a label and logo on images.
I have done some POC on it. Can send you. But its an small web application. Can I paste the whole code here?
--
Thanks
Gaurav K Arya
Gaurav Kr. Arya Please mark as answer or helpful if it helpsThursday, June 2, 2011 12:45 PM -
Hi Gaurav,
If you paste the code here that will be very helpful to me.
thank you
Thursday, June 2, 2011 1:18 PM -
It is not possible to resize a pixel image without losing clarity. A pixel image is any image that is saved as a 2D array of pixels. Only vector graphic images can be resized without losing clarity. Vector graphic images don't store pixels but rather store the information needed to render the image as pixels, which means it can be rendered at any scale without losing clarity.
What kind of image format are you trying to resize?
Evan
Thursday, June 2, 2011 1:57 PM -
Hi Shweta, I am posting all code from aspx pages along with html. Create a POC and check if it fulfills your requirement. //1. The Utility Class using System; using System.Data; using System.Configuration; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Xml.Linq; using System.IO; using System.Windows.Media; using System.Windows.Media.Imaging; /// <summary> /// Summary description for Utility /// </summary> public class Utility { #region Constants public readonly double MAX_WATERMARK_WIDTH_RATIO = 0.5; public readonly double MAX_WATERMARK_HEIGHT_RATIO = 0.20; #endregion public Utility() { } public Stream MainFun(string filePath, double Height, double Width, string OutputType) { Stream s= LoadImageData(filePath,Height,Width,OutputType); return s; } private Stream LoadImageData(string filePath, double Height, double Width, string OutputType) { byte[] byteData = System.IO.File.ReadAllBytes(filePath); Stream fs = new MemoryStream(byteData); BitmapImage bi = new BitmapImage(); BitmapFrame bitmapFrame = GetBitmapFrameFromStream(fs, filePath.Split('.')[1]); System.Drawing.Bitmap objBitmap = new System.Drawing.Bitmap(filePath); double iMaxHeight = Height == 0.0F ? bitmapFrame.Height : Height; double iMaxWidth = Width == 0.0F ? bitmapFrame.Width : Width; RenderTargetBitmap rtBitmap = new RenderTargetBitmap((int)iMaxWidth, (int)iMaxHeight, 72, 72, PixelFormats.Default); bool isResize=Height==0 || Width==0 ?false:true; rtBitmap = this.ResizeImage(rtBitmap, bitmapFrame); string wmImagePath = ConfigurationSettings.AppSettings["WaterMarkFile"]; this.WatermarkImage(rtBitmap, wmImagePath, .7, isResize, iMaxHeight, iMaxWidth); return CreateStream(rtBitmap, filePath.Split('.')[1]); } private BitmapFrame GetBitmapFrameFromStream(Stream bitmapSream, string imageFormat) { BitmapDecoder decoder = BitmapDecoder.Create(bitmapSream,BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.None); if (decoder != null) { return decoder.Frames[0]; } else { return null; } } private Stream CreateStream(RenderTargetBitmap rtb, string preferredFormat) { Stream result = null; BitmapEncoder encoder = null; switch (preferredFormat.ToLower()) { case "jpg": case "jpeg": encoder = new JpegBitmapEncoder(); break; case "bmp": encoder = new BmpBitmapEncoder(); break; case "png": encoder = new PngBitmapEncoder(); break; case "tif": case "tiff": encoder = new TiffBitmapEncoder(); break; case "gif": encoder = new GifBitmapEncoder(); break; case "wmp": encoder = new WmpBitmapEncoder(); break; } encoder.Frames.Add(BitmapFrame.Create(rtb)); Stream stream = new MemoryStream(); //Stream stream = File.Create(@"D:\Gaurav\Images\Modified\abc." + preferredFormat); //stream.Write(info, 0, stream.Length); encoder.Save(stream); result = stream; return result; } private RenderTargetBitmap ResizeImage(RenderTargetBitmap rtBitmap,BitmapFrame bitmapFrame) { DrawingVisual drawVisual = new DrawingVisual(); using (DrawingContext dc = drawVisual.RenderOpen()) { dc.DrawRectangle(new ImageBrush(bitmapFrame), null, new System.Windows.Rect(0, 0, rtBitmap.Width, rtBitmap.Height)); } rtBitmap.Render(drawVisual); return rtBitmap; } private void WatermarkImage(RenderTargetBitmap rtBitmap, string WMImagePath, double watermarkOpacity, bool isResize, double iMaxHeight, double iMaxWidth) { BitmapImage imgWatermark = new BitmapImage(new Uri(WMImagePath)); // isResize = false; double maxWmWidth = (isResize ? iMaxWidth : rtBitmap.Width) * MAX_WATERMARK_WIDTH_RATIO; double maxWmHeight = (isResize ? iMaxHeight : rtBitmap.Height) * MAX_WATERMARK_HEIGHT_RATIO; double percentWmWidth = maxWmWidth / imgWatermark.Width; double percentWmHeight = maxWmHeight / imgWatermark.Height; double idealWmWidth = 0; double idealWmHeight = 0; if (percentWmWidth < percentWmHeight) { idealWmWidth = imgWatermark.Width * percentWmWidth; idealWmHeight = imgWatermark.Height * percentWmWidth; } else { idealWmWidth = imgWatermark.Width * percentWmHeight; idealWmHeight = imgWatermark.Height * percentWmHeight; } double xPosOfWm = ((isResize ? iMaxWidth : rtBitmap.Width) - idealWmWidth) - 10; double yPosOfWm = ((isResize ? iMaxHeight : rtBitmap.Height) - idealWmHeight) - 10; ImageBrush imageBrush = new ImageBrush(); imageBrush.ImageSource = imgWatermark; imageBrush.Stretch = Stretch.Uniform; imageBrush.TileMode = TileMode.None; imageBrush.AlignmentX = AlignmentX.Center; imageBrush.AlignmentY = AlignmentY.Bottom; imageBrush.Opacity = watermarkOpacity; DrawingVisual drawVisual = new DrawingVisual(); using (DrawingContext dc = drawVisual.RenderOpen()) { dc.DrawRectangle(imageBrush, null, new System.Windows.Rect(xPosOfWm, yPosOfWm, idealWmWidth, idealWmHeight)); } rtBitmap.Render(drawVisual); } } //2. The Default Page //From where I pass the raw image and select the //Output Image Type (Ext), Height, Width. //Default.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Select Image</title> </head> <body> <form id="form1" runat="server" > <div height="1000px"> <br /> <br /> Image name <asp:TextBox ID="txtImageName" runat="server" Width="227px"></asp:TextBox> <br /> <br /> Output Type <asp:DropDownList ID="ddlImageType" runat="server" Height="16px" Width="226px"> <asp:ListItem Value=".jpg">Jpeg</asp:ListItem> <asp:ListItem Value=".gif">Gif</asp:ListItem> <asp:ListItem Value=".png">PNG</asp:ListItem> <asp:ListItem Value=".bmp">BMP</asp:ListItem> </asp:DropDownList> <br /> <br /> Width <asp:DropDownList ID="ddlWidth" runat="server"> <asp:ListItem>100</asp:ListItem> <asp:ListItem Text="Same Width" Value="0"></asp:ListItem> <asp:ListItem>200</asp:ListItem> <asp:ListItem>300</asp:ListItem> <asp:ListItem>400</asp:ListItem> <asp:ListItem>500</asp:ListItem> <asp:ListItem>600</asp:ListItem> <asp:ListItem>700</asp:ListItem> <asp:ListItem>800</asp:ListItem> <asp:ListItem>900</asp:ListItem> <asp:ListItem>1000</asp:ListItem> <asp:ListItem>1024</asp:ListItem> <asp:ListItem>1100</asp:ListItem> <asp:ListItem>1200</asp:ListItem> <asp:ListItem>1300</asp:ListItem> <asp:ListItem>1400</asp:ListItem> <asp:ListItem>2000</asp:ListItem> <asp:ListItem>3000</asp:ListItem> <asp:ListItem>4000</asp:ListItem> <asp:ListItem>5000</asp:ListItem> </asp:DropDownList> <br /> <br /> Height <asp:DropDownList ID="ddlHeight" runat="server"> <asp:ListItem Text="Same Height" Value="0"></asp:ListItem> <asp:ListItem>100</asp:ListItem> <asp:ListItem>200</asp:ListItem> <asp:ListItem>300</asp:ListItem> <asp:ListItem>400</asp:ListItem> <asp:ListItem>500</asp:ListItem> <asp:ListItem>600</asp:ListItem> <asp:ListItem>700</asp:ListItem> <asp:ListItem>800</asp:ListItem> <asp:ListItem>900</asp:ListItem> <asp:ListItem>1000</asp:ListItem> <asp:ListItem>1100</asp:ListItem> <asp:ListItem>1108</asp:ListItem> <asp:ListItem>1200</asp:ListItem> <asp:ListItem>1300</asp:ListItem> <asp:ListItem>1400</asp:ListItem> <asp:ListItem>2000</asp:ListItem> <asp:ListItem>3000</asp:ListItem> <asp:ListItem>4000</asp:ListItem> <asp:ListItem>5000</asp:ListItem> </asp:DropDownList> <br /> <br /> <asp:Button ID="btnSame" runat="server" Text="Process with watermark" OnClick="btnSame_Click" Width="229px" /> </div> </form> </body> </html> //3. Default.aspx.cs just writing the button click protected void btnProcessImage_Click(object sender, EventArgs e) { int height = int.Parse(ddlHeight.SelectedValue); int width = int.Parse(ddlWidth.SelectedValue); string outputType = ddlImageType.SelectedValue; if (txtImageName.Text.Trim() != "") { Response.Redirect("Convert.aspx?height=" + height + "&width=" + width + "&outputType=" + outputType + "&ImageName=" + txtImageName.Text.Trim()); } } //4. Convert.aspx Has nothing on html //5. Convert.aspx.cs using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.IO; using System.Drawing; using System.Drawing.Imaging; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; public partial class Convert : System.Web.UI.Page { private readonly string m_sEXT_DWF = "DWF"; private readonly string m_sEXT_PNG = "PNG"; private readonly string m_sEXT_GIF = "GIF"; private readonly string m_sEXT_JPG = "JPG"; private readonly string m_sEXT_TIF = "TIF"; private readonly string m_sEXT_BMP = "BMP"; private readonly string m_sEXT_SVG = "SVG"; private string getMimeType(string a_sGraphicExt) { string mimeType = null; string ext = a_sGraphicExt.Trim().ToUpper(); if (ext.Contains(m_sEXT_DWF)) mimeType = "drawing/x-dwf"; else if (ext.Contains(m_sEXT_PNG)) mimeType = "image/png"; else if (ext.Contains(m_sEXT_GIF)) mimeType = "image/gif"; else if (ext.Contains(m_sEXT_JPG)) mimeType = "image/jpeg"; else if (ext.Contains(m_sEXT_TIF)) mimeType = "image/tiff"; else if (ext.Contains(m_sEXT_BMP)) mimeType = "image/bmp"; else if (ext.Contains(m_sEXT_SVG)) mimeType = "image/svg"; return mimeType; } protected void Page_Load(object sender, EventArgs e) { try { Utility objUtility = new Utility(); Response.Clear(); Response.ClearContent(); Response.ClearHeaders(); try { double height = Double.Parse(Request.QueryString["height"]); double width = Double.Parse(Request.QueryString["width"]); string outputType = Request.QueryString["outputType"].ToString(); string imageName = Request.QueryString["ImageName"].ToString(); string folderPath = ConfigurationSettings.AppSettings["FolderPath"].ToString(); string filePath = folderPath + "\\" + imageName; string fileExt = imageName.Split('.')[1]; string newExtension = string.Empty; Stream st = objUtility.MainFun(filePath,height,width,outputType); Bitmap bmp = new Bitmap(st); MemoryStream ms = new MemoryStream(); bmp.Save(ms, getImageFormat(outputType)); ms.Seek(0, SeekOrigin.Begin); //int initialLength = 2048; byte[] buffer = new byte[ms.Length]; int read = 0; ms.Read(buffer, read, buffer.Length); BinaryWriter bw = new BinaryWriter(Response.OutputStream); bw.Write(buffer); bw.Close(); } catch { } } catch(Exception ex) { Response.Write(ex.Message.ToString()); } } public ImageFormat getImageFormat(string a_sImageFormatStr) { string temp = a_sImageFormatStr.ToUpper(); if (temp.Contains("BMP")) { return ImageFormat.Bmp; } else if (temp.Contains("GIF")) { return ImageFormat.Gif; } else if (temp.Contains("JPG")) { return ImageFormat.Jpeg; } else if (temp.Contains("PNG")) { return ImageFormat.Png; } else if (temp.Contains("TIF")) { return ImageFormat.Tiff; } else { return null; } } } //7. Config Keys set the key values as per your folder location <appSettings> <add key="WaterMarkFile" value="D:\Gaurav\Images\Client_Watermark.png"/> <add key="FolderPath" value="D:\Gaurav\Images\"/> </appSettings> //Note: you will need this dll to run this Namespace: System.Windows.Media.Imaging Assembly: PresentationCore (in presentationcore.dll) Add any if i miss any
--
Thanks & Regards
Gaurav K Arya
Please mark it as answer or helpful if it helps
Gaurav Kr. Arya Please mark as answer or helpful if it helpsThursday, June 2, 2011 2:06 PM -
I am trying to resize pixel images like(.JPG, .BMP, .GIF, .PNG)Friday, June 3, 2011 5:10 AM
-
Any progress Swetha??
Gaurav Kr. Arya Please mark as answer or helpful if it helpsFriday, June 3, 2011 5:34 AM -
Hi SwethaSyam,
Plese do not post dulicated threads. I will merge these threads.:
Vin Jin [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Friday, June 3, 2011 6:08 AM