Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating thumbnail images with C#
    text
    copied!<pre><code>@functions{ public void GetThumbnailView(string originalImagePath, int height, int width) { //Consider Image is stored at path like "ProductImage\\Product1.jpg" //Now we have created one another folder ProductThumbnail to store thumbnail image of product. //So let name of image be same, just change the FolderName while storing image. string thumbnailImagePath = originalImagePath; originalImagePath = originalImagePath.Replace("thumb_", ""); //If thumbnail Image is not available, generate it. if (!System.IO.File.Exists(Server.MapPath(thumbnailImagePath))) { System.Drawing.Image imThumbnailImage; System.Drawing.Image OriginalImage = System.Drawing.Image.FromFile(Server.MapPath(originalImagePath)); double originalWidth = OriginalImage.Width; double originalHeight = OriginalImage.Height; double ratioX = (double)width / (double)originalWidth; double ratioY = (double)height / (double)originalHeight; double ratio = ratioX &lt; ratioY ? ratioX : ratioY; // use whichever multiplier is smaller // now we can get the new height and width int newHeight = Convert.ToInt32(originalHeight * ratio); int newWidth = Convert.ToInt32(originalWidth * ratio); imThumbnailImage = OriginalImage.GetThumbnailImage(newWidth, newHeight, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero); imThumbnailImage.Save(Server.MapPath(thumbnailImagePath), System.Drawing.Imaging.ImageFormat.Jpeg); imThumbnailImage.Dispose(); OriginalImage.Dispose(); } } public bool ThumbnailCallback() { return false; } } </code></pre> <p>in another stackowerflow question i found this code and really liked it but while using it there was a problem occured while creating the thumbnail images as shown below:</p> <blockquote> <h2>Server Error in '/' Application.</h2> <p>Out of memory. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. </p> <p>Exception Details: System.OutOfMemoryException: Out of memory.</p> <p>Source Error: </p> <p>Line 199: {</p> <p>Line 200: System.Drawing.Image imThumbnailImage;</p> <p>Line 201: System.Drawing.Image OriginalImage = System.Drawing.Image.FromFile(Server.MapPath(originalImagePath.ToString()));</p> <p>Line 202:</p> <p>Line 203: double originalWidth = OriginalImage.Width; </p> <p>Source File: c:\Inetpub\wwwroot\Lokal\Views\Stok\SatisRaporu.cshtml<br> Line: 201</p> </blockquote> <p>my curiosity about this issue got me into the exception details and seen this :</p> <pre><code> // // Summary: // Creates an System.Drawing.Image from the specified file. // // Parameters: // filename: // A string that contains the name of the file from which to create the System.Drawing.Image. // // Returns: // The System.Drawing.Image this method creates. // // Exceptions: // System.OutOfMemoryException: // The file does not have a valid image format.-or- GDI+ does not support the // pixel format of the file. // // System.IO.FileNotFoundException: // The specified file does not exist. // // System.ArgumentException: // filename is a System.Uri. [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] public static Image FromFile(string filename); </code></pre> <p>but all my pictures in that folder has ".jpg" extention so it seems wierd to me. if im not gonna be able to create thumbnails from ".jpg" what else i can do?</p> <p>I actually want to learn about if anyone else tried this on ".jpg" files and got a problem with it? and If no problem occured what i might be doing wrong?</p> <p>A little note: I do this in a view using razor syntax. I know a little about c# language and improving my knowledge about it everyday.</p> <p><strong>Edit :</strong></p> <p>How i call the function: </p> <pre><code>GetThumbnailView("../pics/thumb_" + (("0000000" + stocks.stockcode).Substring(("0000000" + stocks.stockcode).Length - 7, 7)) + ".jpg", 200, 200); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload