مرجع تخصصی برنامه نویسان

انجمن تخصصی برنامه نویسان فارسی زبان

کاربر سایت

mrpeach

عضویت از 1394/04/13

خطا در تبدیل کردن سایز عکس

  • دوشنبه 14 اسفند 1396
  • 07:46
تشکر میکنم

با سلام

دوستان من یک کدی رو نوشتم که یک عکس از ورودی میگیره در محلی به صورت موقت ذخیره میکنه و بعد عکس ذخیره شده رو  رو داخل یک bitmap مربع قرار میده و بعد عکس رو ذخیره میکنه. این کد رو برای اینکه عکس هایی که به وب سایت آپلود میشن همه دارای یک شکل یکسان باشند ساختم اما زمانی که کار تمام میشه بدون اینکه عکس ذخیره بشه یک ارور برمیگردونه

public static string UploadImage(DateTime date, HttpPostedFileBase file)
        {
            string fileName = CheckImage(file);
            if (!string.IsNullOrEmpty(fileName))
            {
                string temp = Path.Combine(HttpRuntime.AppDomainAppPath, $"Images\\Temp\\{fileName}");
                string path = Path.Combine(HttpRuntime.AppDomainAppPath, $"Images\\{date.Year}\\{date.Month}\\{date.Day}\\{fileName}");
                file.SaveAs(temp);

                Image original = Bitmap.FromFile(temp);

                int largestDimension = Math.Max(original.Height, original.Width);
                Size squareSize = new Size(largestDimension, largestDimension);
                Bitmap squareImage = new Bitmap(squareSize.Width, squareSize.Height);
                using (Graphics graphics = Graphics.FromImage(squareImage))
                {
                    graphics.FillRectangle(Brushes.Transparent, 0, 0, squareSize.Width, squareSize.Height);
                    graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                    graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                    graphics.DrawImage(original, (squareSize.Width / 2) - (original.Width / 2), (squareSize.Height / 2) - (original.Height / 2), original.Width, original.Height);
                }
                squareImage.Save(path);
            }
            return fileName;
        }

اینجا هم متن خطا رو قرار دادم

A generic error occurred in GDI+.
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. 

Exception Details: System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.

Source Error: 


Line 138:                    graphics.DrawImage(original, (squareSize.Width / 2) - (original.Width / 2), (squareSize.Height / 2) - (original.Height / 2), original.Width, original.Height);
Line 139:                }
Line 140:                squareImage.Save(path);
Line 141:            }
Line 142:            return fileName;

پاسخ های این پرسش

تعداد پاسخ ها : 1 پاسخ
کاربر سایت

ایمان مدائنی

عضویت از 1392/01/20

  • دوشنبه 14 اسفند 1396
  • 09:25

از کلاس زیر استفاده کنید 

using System;
using System.Drawing;
using System.Drawing.Imaging;

namespace InsertShowImage
{
        public class ImageResizer
        {
            /// <summary>
            /// http://www.blackbeltcoder.com/Articles/graph/programmatically-resizing-an-image
            /// Maximum width of resized image.
            /// </summary>
            public int MaxX { get; set; }

            /// <summary>
            /// Maximum height of resized image.
            /// </summary>
            public int MaxY { get; set; }

            /// <summary>
            /// If true, resized image is trimmed to exactly fit
            /// maximum width and height dimensions.
            /// </summary>
            public bool TrimImage { get; set; }

            /// <summary>
            /// Format used to save resized image.
            /// </summary>
            public ImageFormat SaveFormat { get; set; }

            /// <summary>
            /// Constructor.
            /// </summary>
            public ImageResizer()
            {
                MaxX = MaxY = 150;
                TrimImage = false;
                SaveFormat = ImageFormat.Jpeg;
            }

            /// <summary>
            /// Resizes the image from the source file according to the
            /// current settings and saves the result to the targe file.
            /// </summary>
            /// <param name="source">Path containing image to resize</param>
            /// <param name="target">Path to save resized image</param>
            /// <returns>True if successful, false otherwise.</returns>
            public bool Resize(string source, string target)
            {
                using (Image src = Image.FromFile(source, true))
                {
                    // Check that we have an image
                    if (src != null)
                    {
                        int origX, origY, newX, newY;
                        int trimX = 0, trimY = 0;

                        // Default to size of source image
                        newX = origX = src.Width;
                        newY = origY = src.Height;

                        // Does image exceed maximum dimensions?
                        if (origX > MaxX || origY > MaxY)
                        {
                            // Need to resize image
                            if (TrimImage)
                            {
                                // Trim to exactly fit maximum dimensions
                                double factor = Math.Max((double)MaxX / (double)origX,
                                    (double)MaxY / (double)origY);
                                newX = (int)Math.Ceiling((double)origX * factor);
                                newY = (int)Math.Ceiling((double)origY * factor);
                                trimX = newX - MaxX;
                                trimY = newY - MaxY;
                            }
                            else
                            {
                                // Resize (no trim) to keep within maximum dimensions
                                double factor = Math.Min((double)MaxX / (double)origX,
                                    (double)MaxY / (double)origY);
                                newX = (int)Math.Ceiling((double)origX * factor);
                                newY = (int)Math.Ceiling((double)origY * factor);
                            }
                        }

                        // Create destination image
                        using (Image dest = new Bitmap(newX - trimX, newY - trimY))
                        {
                            Graphics graph = Graphics.FromImage(dest);
                            graph.InterpolationMode =
                                System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                            graph.DrawImage(src, -(trimX / 2), -(trimY / 2), newX, newY);
                            dest.Save(target, SaveFormat);
                            // Indicate success
                            return true;
                        }
                    }
                }
                // Indicate failure
                return false;
            }
        }

    }

کاربرانی که از این پست تشکر کرده اند

هیچ کاربری تا کنون از این پست تشکر نکرده است

اگر نیاز به یک مشاور در زمینه طراحی سایت ، برنامه نویسی و بازاریابی الکترونیکی دارید

با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)