PDA

View Full Version : کم کردن حجم عکس



majid_3ma
دوشنبه 11 مرداد 1389, 11:47 صبح
سلام من از این کد برای کوچک کردن عکس استفاده میکنم
مشکلم این هست سایز کوچک میشه حجم نسبتا بالا هستش
چه جوری میشه حجم هم کم کرد؟


string FileToResize = Server.MapPath("~/Images/Orginal/" + txtISN0.Text + ".jpg");
string ResizedFile = Server.MapPath("~/Images/Title/" + txtISN0.Text + ".jpg");
string FileName = txtISN0.Text;
Bitmap orgBMP = new Bitmap(FileUpload1.FileContent);
double orgWidth = orgBMP.Width;
double orgHeigh = orgBMP.Height;
int newWidth = Convert.ToInt32(orgBMP.Width);
double bmpraito = (orgHeigh / orgWidth) * newWidth;
int newHeight = orgBMP.Height;
Bitmap newBMP = new Bitmap(orgBMP, newWidth, newHeight);
Graphics OGraphic = Graphics.FromImage(newBMP);
OGraphic.SmoothingMode = SmoothingMode.AntiAlias;
OGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
OGraphic.DrawImage(orgBMP, 0, 0, newWidth, newHeight);
newBMP.Save(FileToResize, ImageFormat.Jpeg);
orgBMP.Dispose();
newBMP.Dispose();
OGraphic.Dispose();

Mostafa_Dindar
دوشنبه 11 مرداد 1389, 19:56 عصر
سلام من از این کد برای کوچک کردن عکس استفاده میکنم
مشکلم این هست سایز کوچک میشه حجم نسبتا بالا هستش
چه جوری میشه حجم هم کم کرد؟


string FileToResize = Server.MapPath("~/Images/Orginal/" + txtISN0.Text + ".jpg");
string ResizedFile = Server.MapPath("~/Images/Title/" + txtISN0.Text + ".jpg");
string FileName = txtISN0.Text;
Bitmap orgBMP = new Bitmap(FileUpload1.FileContent);
double orgWidth = orgBMP.Width;
double orgHeigh = orgBMP.Height;
int newWidth = Convert.ToInt32(orgBMP.Width);
double bmpraito = (orgHeigh / orgWidth) * newWidth;
int newHeight = orgBMP.Height;
Bitmap newBMP = new Bitmap(orgBMP, newWidth, newHeight);
Graphics OGraphic = Graphics.FromImage(newBMP);
OGraphic.SmoothingMode = SmoothingMode.AntiAlias;
OGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
OGraphic.DrawImage(orgBMP, 0, 0, newWidth, newHeight);
newBMP.Save(FileToResize, ImageFormat.Jpeg);
orgBMP.Dispose();
newBMP.Dispose();
OGraphic.Dispose();


فکر میکنم با مطالعه این مقاله (http://dotnetslackers.com/articles/aspnet/Generating-Image-Thumbnails-in-ASP-NET.aspx), مشکل شما برطرف خواهد شد .

موفق باشید

VB.NET 2010
دوشنبه 11 مرداد 1389, 23:35 عصر
مقاله رو بخون !
به جای jpg از فرمت gif استفاده کنی حجم کمتر میشه

majid_3ma
سه شنبه 12 مرداد 1389, 08:48 صبح
مقاله رو بخون !
به جای jpg از فرمت gif استفاده کنی حجم کمتر میشه
اینو میدونم
ولی یک سایت بود که با فرمت jpg آپلود میشد حجمش خیلی کم میشد

ricky22
سه شنبه 12 مرداد 1389, 08:57 صبح
سلام/
این (http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/647d3fd4-f435-43ef-a408-65635a8d1a9e)و این (http://www.velocityreviews.com/forums/t123522-how-to-reduce-image-file-size.html)و نگاه کن
کد زیر از عکس شما یک thumbnailImage می سازه
using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.IO;

using System.Drawing;



/// <summary>

/// Summary description for makeThumbnail

/// </summary>

public class makeThumbnail

{

public makeThumbnail(string parmFileIn, out int rc)

{

rc = 0;

string filePathSourceFile = HttpContext.Current.Server.MapPath("~/images/");

string filePathDestFile =
HttpContext.Current.Server.MapPath("~/thumbnails/");

string fullSourceFile = filePathSourceFile + parmFileIn;

//string currentImageFile;

//string thumbnailFile;

int thumbnailHeight;

int thumbnailWidth;

int maxThumbSize = 150;

//int minThumbSize = 120; // 4 x 6 ratio

double hx; double wx;

// create an image object, using the filename we just retrieved

System.Drawing.Image sourceImage =
System.Drawing.Image.FromFile(fullSourceFile);

hx = sourceImage.Height;

wx = sourceImage.Width;

// calc the thumbnail Width and Height: -scale to max with

if (sourceImage.Width > sourceImage.Height) // landscape: width is fixed,
scale height:

{

thumbnailWidth = maxThumbSize;

//thumbnailHeight = minThumbSize;

thumbnailHeight = Convert.ToInt32((hx / wx) * thumbnailWidth);

}

else // portrait: height is fixed at max, scale width:

{

thumbnailHeight = maxThumbSize;

thumbnailWidth = Convert.ToInt32((wx / hx) * thumbnailHeight);

}

System.Drawing.Image thumbnailImage =
sourceImage.GetThumbnailImage(thumbnailWidth, thumbnailHeight, new
System.Drawing.Image.GetThumbnailImageAbort(Thumbn ailCallback),
IntPtr.Zero);


// make a memory stream to work with the image bytes

MemoryStream imageStream = new MemoryStream();

// put the image into the memory stream

thumbnailImage.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg);

string outfile = filePathDestFile + parmFileIn;

try

{

thumbnailImage.Save(outfile);

rc = 77;

}

catch (Exception e)

{

rc = 27;

string dummy = e.ToString();

return;

}

}

/// <summary>

/// </summary>

/// <returns>true</returns>

public bool ThumbnailCallback()

{

return true;

}


}