本文整理汇总了C#中Bitmap.GetThumbnailImage方法的典型用法代码示例。如果您正苦于以下问题:C# Bitmap.GetThumbnailImage方法的具体用法?C# Bitmap.GetThumbnailImage怎么用?C# Bitmap.GetThumbnailImage使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Bitmap
的用法示例。
在下文中一共展示了Bitmap.GetThumbnailImage方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateCircleImageStream
public static Stream CreateCircleImageStream(Stream imgStream, int circleDiameter)
{
//convert stream to bitmap
Bitmap imgBitmap = new Bitmap(imgStream);
//for debug
//saveImage(imgBitmap, "3 inputsteam to bitmap.jpg");
//create squre image with circle drawn
imgBitmap = CreateSqureImageAndDrawCicle(imgBitmap, circleDiameter * resizeScale);
//create circle image
imgBitmap = CropImageToCircle(imgBitmap);
//for debug
//saveImage(imgBitmap, "6 circle croped.jpg");
//reduce size
System.Drawing.Image resizedImage = imgBitmap.GetThumbnailImage(circleDiameter, circleDiameter, null, System.IntPtr.Zero);
//debug
//saveImage(resizedImage, "7 resized.jpg");
//convert bitmap to stream
Stream outputStream = new MemoryStream();
resizedImage.Save(outputStream, System.Drawing.Imaging.ImageFormat.Png);//jpg does not support transparency
resizedImage.Dispose();
imgBitmap.Dispose();
return outputStream;
}
示例2: ProcessRequest
public void ProcessRequest(HttpContext context)
{
string ImageUrl = context.Request.QueryString["url"];
//get image width to be resized from querystring
string ImageWidth = context.Request.QueryString["width"];
//get image height to be resized from querystring
string ImageHeight = context.Request.QueryString["height"];
if (ImageUrl != string.Empty && ImageHeight != string.Empty && ImageWidth != string.Empty && (ImageWidth != null && ImageHeight != null))
{
if (!System.Web.UI.WebControls.Unit.Parse(ImageWidth).IsEmpty && !System.Web.UI.WebControls.Unit.Parse(ImageHeight).IsEmpty)
{
Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback);
//creat BitMap object from image path passed in querystring
Bitmap BitmapForImage = new Bitmap(context.Server.MapPath(ImageUrl));
//create unit object for height and width. This is to convert parameter passed in differen unit like pixel, inch into generic unit.
System.Web.UI.WebControls.Unit WidthUnit = System.Web.UI.WebControls.Unit.Parse(ImageWidth);
System.Web.UI.WebControls.Unit HeightUnit = System.Web.UI.WebControls.Unit.Parse(ImageHeight);
//Resize actual image using width and height paramters passed in querystring
Image CurrentThumbnail = BitmapForImage.GetThumbnailImage(Convert.ToInt16(WidthUnit.Value), Convert.ToInt16(HeightUnit.Value), myCallback, IntPtr.Zero);
//Create memory stream and save resized image into memory stream
MemoryStream objMemoryStream = new MemoryStream();
if (ImageUrl.EndsWith(".png"))
{
context.Response.ContentType = "image/png";
CurrentThumbnail.Save(objMemoryStream, System.Drawing.Imaging.ImageFormat.Png);
}
else if (ImageUrl.EndsWith(".jpg"))
{
context.Response.ContentType = "image/jpeg";
CurrentThumbnail.Save(objMemoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
else if (ImageUrl.EndsWith(".gif"))
{
context.Response.ContentType = "image/gif";
CurrentThumbnail.Save(objMemoryStream, System.Drawing.Imaging.ImageFormat.Gif);
}
//Declare byte array of size memory stream and read memory stream data into array
byte[] imageData = new byte[objMemoryStream.Length];
objMemoryStream.Position = 0;
objMemoryStream.Read(imageData, 0, (int)objMemoryStream.Length);
//send contents of byte array as response to client (browser)
context.Response.BinaryWrite(imageData);
}
}
else
{
//if width and height was not passed as querystring, then send image as it is from path provided.
context.Response.WriteFile(context.Server.MapPath(ImageUrl));
}
}
示例3: Main
static void Main(string[] args)
{
//入力JPEG読み込み
var inputImg = new Bitmap(args[1]);
//変換後の横幅
int toWidth = int.Parse(args[0]);
double rate = (double)toWidth / inputImg.Width;
//変換後の縦幅
int toHeight = (int)(inputImg.Height * rate);
//サムネイル画像作成
var outputImg = inputImg.GetThumbnailImage(toWidth, toHeight, () => true, new IntPtr());
//サムネイル画像出力
outputImg.Save(args[2]);
}
示例4: UploadImage
public static bool UploadImage(Stream stream, string filename)
{
System.Drawing.Image image = null;
try
{
bool bCreateThumb = false;
image = new Bitmap(stream);
int width = image.Width;
int height = image.Height;
if (width > 240)
{
bCreateThumb = true;
height = (int)(height * 240 / width);
width = 240;
}
if (height > 180)
{
bCreateThumb = true;
width = (int)(width * 180 / height);
height = 180;
}
if (bCreateThumb)
image = image.GetThumbnailImage(width, height, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
if (File.Exists(filename))
File.Delete(filename);
image.Save(filename);
}
catch
{
return false;
}
finally
{
image = null;
}
return true;
}
示例5: Button1_Click
//private const string savePath = @"F:\WebDegsin\作品集\According2\Data_Temp\";
//private const string tempPath = @"F:\WebDegsin\作品集\According2\img\";
protected void Button1_Click(object sender, EventArgs e)
{
string tempPath = Server.MapPath("~/Data_Temp/");
string savePath = Server.MapPath("~/img/");
if (FileUpload1.HasFile)
{
string tempName = tempPath + FileUpload1.FileName;
string imageName = savePath + FileUpload1.FileName;
// 儲存暫存檔
FileUpload1.SaveAs(tempName);
// System.Web.UI.WebControls 與 System.Drawing 同時擁有 Image 類別
// 所以以下程式碼明確指定要使用的是 System.Drawing.Image
System.Drawing.Image.GetThumbnailImageAbort callBack = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
Bitmap image = new Bitmap(tempName);
// 計算維持比例的縮圖大小
int[] thumbnailScale = getThumbnailImageScale(600, 600, image.Width, image.Height);
// 產生縮圖
System.Drawing.Image smallImage = image.GetThumbnailImage(thumbnailScale[0], thumbnailScale[1], callBack, IntPtr.Zero);
// 將縮圖存檔
smallImage.Save(imageName);
// 釋放並刪除暫存檔
image.Dispose();
System.IO.File.Delete(tempName);
Response.Write("ok");
}
else
{
Response.Write("error");
}
}
示例6: ResizeImage
public static Bitmap ResizeImage(Bitmap fullsizeImage, int maxWidth, int maxHeight, string imageName)
{
fullsizeImage.RotateFlip(RotateFlipType.Rotate180FlipNone);
fullsizeImage.RotateFlip(RotateFlipType.Rotate180FlipNone);
int imageWidth;
int imageHeight;
var newWidth = maxHeight * fullsizeImage.Width / fullsizeImage.Height;
if (newWidth > maxWidth)
{
imageHeight = fullsizeImage.Height * maxWidth / fullsizeImage.Width;
imageWidth = maxWidth;
}
else
{
imageWidth = newWidth;
imageHeight = maxHeight;
}
System.Drawing.Image newImage = fullsizeImage.GetThumbnailImage(imageWidth, imageHeight, null, IntPtr.Zero);
fullsizeImage.Dispose();
return (Bitmap)newImage;
}
示例7: GenThumb
private byte[] GenThumb(byte[] buffer, ImageFormat format, string type)
{
MemoryStream iMS = new MemoryStream(buffer);
System.Drawing.Image img = new Bitmap(iMS);
int h = -1;
switch (type)
{
case "n":
h = img.Height / (img.Width / 128);
break;
case "t":
h = 96;
break;
default:
break;
}
System.Drawing.Image thumb = img.GetThumbnailImage(128, h, null, new IntPtr());
MemoryStream tMS = new MemoryStream();
thumb.Save(tMS, format);
//Array.Resize(ref buffer, 0);
return tMS.ToArray();
}
示例8: CreateThumbnail
private string CreateThumbnail(string path, string filename)
{
System.Drawing.Image myThumbnail150;
System.Drawing.Image imagesize = System.Drawing.Image.FromFile(path);
Bitmap bitmapNew = new Bitmap(imagesize);
if (imagesize.Width < imagesize.Height)
myThumbnail150 = bitmapNew.GetThumbnailImage(150 * imagesize.Width / imagesize.Height, 150, myCallback, IntPtr.Zero);
else
myThumbnail150 = bitmapNew.GetThumbnailImage(150, imagesize.Height * 150 / imagesize.Width, myCallback, IntPtr.Zero);
myThumbnail150.Save(ConfigurationManager.AppSettings["LogoUploadLocation"] + Request.QueryString["OrganizationId"] + "/" + filename + "-t.png", System.Drawing.Imaging.ImageFormat.Png);
return filename + "-t.png";
}
示例9: SaveBannerContent
private void SaveBannerContent(int BannerId, int ImageId)
{
try
{
bool isEdit = false;
SageBannerInfo obj = new SageBannerInfo();
if (Session["EditImageID"] != null && Session["EditImageID"].ToString() != string.Empty)
{
obj.ImageID = Int32.Parse(Session["EditImageID"].ToString());
if (fuFileUpload.HasFile)
{
obj.ImagePath = fuFileUpload.PostedFile.FileName.Replace(" ", "_");
obj.NavigationImage = fuFileUpload.PostedFile.FileName.Replace(" ", "_");
}
else
{
isEdit = true;
obj.ImagePath = Convert.ToString(Session["ImageName"]);
obj.NavigationImage = Convert.ToString(Session["ImageName"]);
}
}
else
{
obj.ImageID = 0;
obj.ImagePath = fuFileUpload.FileName.Replace(" ", "_");
obj.NavigationImage = fuFileUpload.FileName.Replace(" ", "_");
}
obj.Caption = string.Empty;
if (rdbReadMorePageType.SelectedItem.Text == "Page")
{
obj.ReadMorePage = ddlPagesLoad.SelectedValue.ToString();
obj.LinkToImage = string.Empty;
}
if (rdbReadMorePageType.SelectedItem.Text == "Web Url")
{
obj.LinkToImage = txtWebUrl.Text;
obj.ReadMorePage = string.Empty;
}
obj.UserModuleID = Int32.Parse(SageUserModuleID);
obj.BannerID = BannerId;
obj.ImageID = ImageId;
obj.ReadButtonText = txtReadButtonText.Text;
obj.Description = txtBannerDescriptionToBeShown.Text.Trim();
obj.PortalID = GetPortalID;
obj.CultureCode = GetCurrentCulture();
string swfExt = System.IO.Path.GetExtension(fuFileUpload.PostedFile.FileName);
if (swfExt == ".swf")
{
if (fuFileUpload.FileContent.Length > 0)
{
string Path = GetUplaodImagePhysicalPath();
string fileName = fuFileUpload.PostedFile.FileName.Replace(" ", "_");
DirectoryInfo dirUploadImage = new DirectoryInfo(Path);
if (dirUploadImage.Exists == false)
{
dirUploadImage.Create();
}
string fileUrl = Path + fileName;
int i = 1;
while (File.Exists(fileUrl))
{
fileName = i + fileName;
fileUrl = Path + i + fileName;
i++;
}
fuFileUpload.PostedFile.SaveAs(fileUrl);
swfFileName = "Modules/Sage_Banner/images/" + fileName;
obj.ImagePath = fileName;
obj.NavigationImage = fileName;
}
}
else
{
string target = Server.MapPath("~/Modules/Sage_Banner/images/OriginalImage");
string CropImageTarget = Server.MapPath("~/Modules/Sage_banner/images/CroppedImages");
string thumbTarget = Server.MapPath("~/Modules/Sage_Banner/images/ThumbNail");
System.Drawing.Image.GetThumbnailImageAbort thumbnailImageAbortDelegate = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
if (fuFileUpload.HasFile)
{
string fileName = fuFileUpload.PostedFile.FileName.Replace(" ", "_");
int i = 1;
while (File.Exists(target + "/" + fileName))
{
fileName = i + fileName;
i++;
}
fuFileUpload.SaveAs(Path.Combine(target, fileName));
fuFileUpload.SaveAs(Path.Combine(CropImageTarget, fileName));
using (Bitmap originalImage = new Bitmap(fuFileUpload.PostedFile.InputStream))
{
using (System.Drawing.Image thumbnail = originalImage.GetThumbnailImage(80, 80, thumbnailImageAbortDelegate, IntPtr.Zero))
{
thumbnail.Save(Path.Combine(thumbTarget, fileName));
}
}
obj.ImagePath = fileName;
obj.NavigationImage = fileName;
}
}
//.........这里部分代码省略.........
示例10: ProcessRequest
public void ProcessRequest(HttpContext ctx)
{
// let's cache this for 1 day//no cache ŞY
ctx.Response.ContentType = "image/jpeg";
//ctx.Response.Cache.SetCacheability(HttpCacheability.Public);
//ctx.Response.Cache.SetExpires(DateTime.Now.AddDays(1));
// find the directory where we're storing the images
//string imageDir = ConfigurationSettings.AppSettings["imageDir"];
//imageDir = Path.Combine(
// ctx.Request.PhysicalApplicationPath, imageDir);
// find the image that was requested
//string file = ctx.Request.PhysicalApplicationPath + path;
string file = path;
// file = Path.Combine(imageDir, file);
// load it up
using (System.Drawing.Image img = new Bitmap(file))
{
// do some math to resize the image
// note: i know very little about image resizing,
// but this just seemed to work under v1.1. I think
// under v2.0 the images look incorrect.
// *note to self* fix this for v2.0
//int h = img.Height;
//int w = img.Width;
//int b = h > w ? h : w;
//double per = (b > MaxDim) ? (MaxDim * 1.0) / b : 1.0;
//h = 136;//(int)(h * per);
//w = 184;//(int)(w * per);
// create the thumbnail image
using (System.Drawing.Image img2 =
img.GetThumbnailImage(width, height,
new System.Drawing.Image.GetThumbnailImageAbort(Abort),
IntPtr.Zero))
{
// emit it to the response strea,
img2.Save(ctx.Response.OutputStream,
System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
示例11: SaveThumbnail
/// <summary>
/// To create new image with new dimensions.
/// </summary>
/// <param name="smallImgPath">Source Img</param>
/// <param name="bigImgPath">Destination Img</param>
/// <param name="width">New Width</param>
/// <param name="height">New Height</param>
private void SaveThumbnail(string smallImgPath, string bigImgPath, int width, int height)
{
Encoder myEncoder;
EncoderParameter myEncoderParameter;
EncoderParameters myEncoderParameters=new EncoderParameters();
ImageCodecInfo myImageCodecInfo;
// Get an ImageCodecInfo object that represents the JPEG codec.
myImageCodecInfo = GetEncoderInfo("image/png");
System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
Bitmap myBitmap = new Bitmap(smallImgPath);
// for the Quality parameter category.
myEncoder = Encoder.Quality;
// Save the bitmap as a JPEG file with quality level 50.
myEncoderParameter = new EncoderParameter(myEncoder, 100L);
myEncoderParameters.Param[0] = myEncoderParameter;
int imageHeight = (int)((width * myBitmap.Height) / myBitmap.Width);
int imageWidth = (int)((height * myBitmap.Width) / myBitmap.Height);
System.Drawing.Image myThumbnail = myBitmap.GetThumbnailImage(imageWidth, height, myCallback, IntPtr.Zero);
myThumbnail.Save(bigImgPath, myImageCodecInfo, myEncoderParameters);
myBitmap.Dispose();
myThumbnail.Dispose();
}
示例12: GalleryImagesData
public byte[][] GalleryImagesData(string target, string legal)
{
byte[][] buffer = { };
try
{
string tbl = "pics";
string sqlStr = "SELECT * FROM " + tbl;
OleDbConnection cnn = new OleDbConnection(Base.cnnStrPics);
cnn.Open();
OleDbCommand cmd = new OleDbCommand(sqlStr, cnn);
OleDbDataReader drr = cmd.ExecuteReader();
while (drr.Read())
{
if (drr["location"].ToString().Trim() == target.ToString().Trim())
{
int len = buffer.Length;
Array.Resize(ref buffer, len + 1);
MemoryStream iMS = new MemoryStream(Convert.FromBase64String(drr["data"].ToString()));
Image img = new Bitmap(iMS);
Image thumb = img.GetThumbnailImage(128, 96, null, new IntPtr());
MemoryStream tMS = new MemoryStream();
thumb.Save(tMS, ImageFormat.Jpeg);
buffer[len] = tMS.ToArray();
}
}
cnn.Close();
drr.Close();
cmd.Dispose();
drr.Dispose();
cnn.Dispose();
cmd = null;
drr = null;
cnn = null;
}
catch
{
}
finally
{
}
return buffer;
}
示例13: CreateSqureImageAndDrawCicle
private static Bitmap CreateSqureImageAndDrawCicle(Bitmap fullSizeImg, int Size)
{
int width = fullSizeImg.Width;
int height = fullSizeImg.Height;
int x = 0;
int y = 0;
//Determine dimensions of resized version of the image
if (width > height)
{
width = (int)(width / height) * Size;
height = Size;
// moves cursor so that crop is more centered
x = Convert.ToInt32(Math.Ceiling((double)((width - height) / 2)));
}
else if (height > width)
{
height = (int)(height / width) * Size;
width = Size;
// moves cursor so that crop is more centered
y = Convert.ToInt32(Math.Ceiling((double)((height - width) / 2)));
}
else
{
width = Size;
height = Size;
}
//First Resize the Existing Image
System.Drawing.Image thumbNailImg = fullSizeImg.GetThumbnailImage(width, height, null, System.IntPtr.Zero);
//debug
//saveImage(thumbNailImg, "4 thumbNailImg.jpg");
//Clean up / Dispose...
fullSizeImg.Dispose();
//Create a Crop Frame to apply to the Resized Image
Bitmap myBitmapCropped = new Bitmap(width, height);//PixelFormat.Format16bppRgb555
myBitmapCropped.SetResolution(thumbNailImg.HorizontalResolution, thumbNailImg.VerticalResolution);
Graphics myGraphic = FromImage(myBitmapCropped);
//Apply the Crop to the Resized Image
myGraphic.DrawImage(thumbNailImg, new Rectangle(0, 0, myBitmapCropped.Width, myBitmapCropped.Height),
x, y, myBitmapCropped.Width, myBitmapCropped.Height, GraphicsUnit.Pixel);
//draw boarder
myGraphic.DrawEllipse(new Pen(Color.White, borderSize),
new Rectangle(0, 0, myBitmapCropped.Width, myBitmapCropped.Height));
//Clean up / Dispose...
myGraphic.Dispose();
//debug
//saveImage(myBitmapCropped, "5 circle drawn.jpg");
//Clean up / Dispose...
thumbNailImg.Dispose();
return myBitmapCropped;
}
示例14: CreateThumbnail
private string CreateThumbnail(string path, string filename, int orgId)
{
try
{
System.Drawing.Image myThumbnail150;
System.Drawing.Image imagesize = System.Drawing.Image.FromFile(path);
Bitmap bitmapNew = new Bitmap(imagesize);
if (imagesize.Width < imagesize.Height)
myThumbnail150 = bitmapNew.GetThumbnailImage(100 * imagesize.Width / imagesize.Height, 100, myCallback, IntPtr.Zero);
else
myThumbnail150 = bitmapNew.GetThumbnailImage(100, imagesize.Height * 100 / imagesize.Width, myCallback, IntPtr.Zero);
myThumbnail150.Save(Server.MapPath(ConfigurationManager.AppSettings["LogoUploadLocation"] + orgId.ToString() + "/" + filename + "-t.png"), System.Drawing.Imaging.ImageFormat.Png);
return filename + "-t.png";
}
catch (Exception ex)
{
new SqlLog().InsertSqlLog(0, "logo-setting.aspx CreateThumbnail", ex);
return null;
}
}
示例15: GetThumnailImage
public static Bitmap GetThumnailImage(Bitmap OriginalImage, int MaxExtent)
{
float AspectRatio = OriginalImage.Width / OriginalImage.Height;
float SizeRatio;
int NewWidth, NewHeight;
if (AspectRatio == 0)
return (Bitmap)OriginalImage.GetThumbnailImage(MaxExtent, MaxExtent, null, IntPtr.Zero);
else if (AspectRatio > 1)
{
if (OriginalImage.Width < MaxExtent)
return (Bitmap)OriginalImage.Clone();
SizeRatio = MaxExtent / OriginalImage.Width;
}
else //AspectRation >1
{
if (OriginalImage.Height < MaxExtent)
return (Bitmap)OriginalImage.Clone();
SizeRatio = MaxExtent / OriginalImage.Height;
}
NewWidth = (int)((float)OriginalImage.Width * SizeRatio);
NewHeight = (int)((float)OriginalImage.Height * SizeRatio);
return GetThumnailImage(OriginalImage, NewWidth, NewHeight);
}