PDA

View Full Version : کار با کلاس Image



peymannaji
جمعه 01 آبان 1388, 14:12 عصر
با سلام

من تکه کد زیر رو دارم :




Dim url As String = Request.QueryString("url").ToString
Dim type As String = Request.QueryString("type").ToString
Dim pagerank As New pagerank(url)
Dim rankNumber As String = pagerank.PageRank.ToString
Dim img As System.Drawing.Image
img = System.Drawing.Image.FromFile(Server.MapPath("~/pagerank/image/prs_" & type & "/" & rankNumber & ".gif"))
img.Save(Response.OutputStream, ImageFormat.Gif)


2 سوال دارم :

1- چگونه میتونم برای آبجکت Image طول و عرض تعیین کنم ؟ چرا که با هر روشی از جمله Unit کار کردم جواب نداد .

2- چگونه میتونم از OutPutStream ایجاد شده طول و عرض واقعی تصویر رو بدست بیارم ؟


با تشکر ...

peymannaji
شنبه 02 آبان 1388, 20:11 عصر
دوستان کسی نیست راهنمایی کنه واقعا نیازمندیم :افسرده:

Saber_Fatholahi
یک شنبه 03 آبان 1388, 11:31 صبح
سلام دوست من با یه سرچ اینارو واست گیر اوردم امیدوارم کارت را بیفته

کد 1

imgPhoto = ScaleByPercent(imgPhotoVert, 50);imgPhoto.Save(WorkingDirectory + @"\images\imageresize_1.jpg", ImageFormat.Jpeg);
imgPhoto.Dispose();
....
static Image ScaleByPercent(Image imgPhoto, int Percent)
{
float nPercent = ((float)Percent/100);

int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;

int destX = 0;
int destY = 0;
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);

Bitmap bmPhoto = new Bitmap(destWidth, destHeight,
PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolutio n,
imgPhoto.VerticalResolution);

Graphics grPhoto = Graphics.FromImage(bmPhoto);
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;
}

کد 2

imgPhoto = FixedSize(imgPhotoVert, 300, 300);
imgPhoto.Save(WorkingDirectory +
@"\images\imageresize_3.jpg", ImageFormat.Jpeg);
imgPhoto.Dispose();
....
static Image FixedSize(Image imgPhoto, int Width, int Height)
{
int sourceWidth = imgPhoto.Width;
int sourceHeight = 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.HorizontalResolutio n,
imgPhoto.VerticalResolution);

Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.Clear(Color.Red);
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;
}

کد 3 از فروم مایکروسافت

public static byte[] ResizeFromByteArray(string fileName, int MaxSideSize, Byte[] byteArrayIn)
{
byte[] byteArray = null; // really make this an error gif
MemoryStream ms = new MemoryStream(byteArrayIn);
byteArray = CodeCampSV.Utils.ResizeFromStream(fileName, MaxSideSize, ms);



return byteArray;
}

/// <summary>
/// converts from input stream to output bytearray
/// inspired from: http://www.eggheadcafe.com/articles/20030515.asp
/// </summary>
/// <param name="ImageSavePath"></param>
/// <param name="MaxSideSize"></param>
/// <param name="Buffer"></param>
/// <returns></returns>
public static byte[] ResizeFromStream(string fileName, int MaxSideSize, Stream Buffer)
{
byte[] byteArray = null; // really make this an error gif

try
{




Bitmap bitMap = new Bitmap(Buffer);
int intOldWidth = bitMap.Width;
int intOldHeight = bitMap.Height;

int intNewWidth;
int intNewHeight;

int intMaxSide;

if (intOldWidth >= intOldHeight)
{
intMaxSide = intOldWidth;
}
else
{
intMaxSide = intOldHeight;
}

if (intMaxSide > MaxSideSize)
{
//set new width and height
double dblCoef = MaxSideSize / (double)intMaxSide;
intNewWidth = Convert.ToInt32(dblCoef * intOldWidth);
intNewHeight = Convert.ToInt32(dblCoef * intOldHeight);
}
else
{
intNewWidth = intOldWidth;
intNewHeight = intOldHeight;
}

Size ThumbNailSize = new Size(intNewWidth, intNewHeight);
System.Drawing.Image oImg = System.Drawing.Image.FromStream(Buffer);
System.Drawing.Image oThumbNail = new Bitmap
(ThumbNailSize.Width, ThumbNailSize.Height);
Graphics oGraphic = Graphics.FromImage(oThumbNail);
oGraphic.CompositingQuality = CompositingQuality.HighQuality;
oGraphic.SmoothingMode = SmoothingMode.HighQuality;
oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
Rectangle oRectangle = new Rectangle
(0, 0, ThumbNailSize.Width, ThumbNailSize.Height);

oGraphic.DrawImage(oImg, oRectangle);

//string fileName = Context.Server.MapPath("~/App_Data/") + "test1.jpg";
//oThumbNail.Save(fileName, ImageFormat.Jpeg);
MemoryStream ms = new MemoryStream();
oThumbNail.Save(ms, ImageFormat.Jpeg);
byteArray = new byte[ms.Length];
ms.Position = 0;
ms.Read(byteArray, 0, Convert.ToInt32(ms.Length));

oGraphic.Dispose();
oImg.Dispose();
ms.Close();
ms.Dispose();
}
catch (Exception)
{
int newSize = MaxSideSize - 20;
Bitmap bitMap = new Bitmap(newSize, newSize);
Graphics g = Graphics.FromImage(bitMap);
g.FillRectangle(new SolidBrush(Color.Gray), new Rectangle(0, 0, newSize, newSize));

Font font = new Font("Courier", 8);
SolidBrush solidBrush = new SolidBrush(Color.Red);
g.DrawString("Failed File", font, solidBrush, 10, 5);
g.DrawString(fileName, font, solidBrush, 10, 50);

MemoryStream ms = new MemoryStream();
bitMap.Save(ms, ImageFormat.Jpeg);
byteArray = new byte[ms.Length];
ms.Position = 0;
ms.Read(byteArray, 0, Convert.ToInt32(ms.Length));

ms.Close();
ms.Dispose();
bitMap.Dispose();
solidBrush.Dispose();
g.Dispose();
font.Dispose();

}
return byteArray;
}

موفق باشی
مارو هم دعا کن

peymannaji
یک شنبه 03 آبان 1388, 20:15 عصر
آقا شرمنده فرمودید . واقعا ممنون مثال های بسیار مفیدی بود . :قلب:

alireza_s_84
یک شنبه 03 آبان 1388, 20:55 عصر
آقا شرمنده فرمودید . واقعا ممنون مثال های بسیار مفیدی بود . :قلب:
سلام دوست عزیز:
شما میتونید یک شی جدید Bitmap تعریف کنید که در اون طول و عرض مورد نظرتون رو ست کنید بعد این شی bitmap رو با عکس مورد نظرتون پر کنید بعد به جای نوشتن اون عکس این Bitmap رو روی Stream بریزید.



Bitmap bmp = new Bitmap(Image.FromFile(Server.MapPath("~/pagerank/image/prs_" + type + "/" + rankNumber + ".gif")), new Size(200, 200));
bmp.Save(Response.OutputStream, ImageFormat.Gif);


موفق باشید