本文整理汇总了C#中System.Web.Helpers.WebImage.Crop方法的典型用法代码示例。如果您正苦于以下问题:C# WebImage.Crop方法的具体用法?C# WebImage.Crop怎么用?C# WebImage.Crop使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Web.Helpers.WebImage
的用法示例。
在下文中一共展示了WebImage.Crop方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Save
public ActionResult Save(string t, string l, string h, string w, string fileName)
{
try
{
// Calculate dimensions
var top = Convert.ToInt32(t.Replace("-", "").Replace("px", ""));
var left = Convert.ToInt32(l.Replace("-", "").Replace("px", ""));
var height = Convert.ToInt32(h.Replace("-", "").Replace("px", ""));
var width = Convert.ToInt32(w.Replace("-", "").Replace("px", ""));
// Get file from temporary folder
var fn = Path.Combine(Server.MapPath(MapTempFolder), Path.GetFileName(fileName));
// ...get image and resize it, ...
var img = new WebImage(fn);
img.Resize(width, height);
// ... crop the part the user selected, ...
img.Crop(top, left, img.Height - top - AvatarStoredHeight, img.Width - left - AvatarStoredWidth);
// ... delete the temporary file,...
System.IO.File.Delete(fn);
// ... and save the new one.
var newFileName = Path.Combine(AvatarPath, Path.GetFileName(fn));
var newFileLocation = HttpContext.Server.MapPath(newFileName);
if (Directory.Exists(Path.GetDirectoryName(newFileLocation)) == false)
{
Directory.CreateDirectory(Path.GetDirectoryName(newFileLocation));
}
img.Save(newFileLocation);
return Json(new { success = true, avatarFileLocation = newFileName });
}
catch (Exception ex)
{
return Json(new { success = false, errorMessage = "Unable to upload file.\nERRORINFO: " + ex.Message });
}
}
示例2: EditImage
public ActionResult EditImage(EditorInputModel editor)
{
string fileName = editor.Profile.ImageUrl;
var image = new WebImage(HttpContext.Server.MapPath("/Images/Temp/") + fileName);
double ratio = editor.Width / 620;
//the values to crop off.
double top = editor.Top * ratio;
double left = editor.Left * ratio;
double bottom = editor.Height - editor.Bottom * ratio;
double right = editor.Width - editor.Right * ratio;
image.Crop((int)top, (int)left, (int)bottom, (int)right);
//the image size I need at the end
image.Resize(620, 280);
image.Save(Path.Combine(HttpContext.Server.MapPath("/Images/News"), fileName));
System.IO.File.Delete(Path.Combine(HttpContext.Server.MapPath("/Images/Temp/"), fileName));
var imageThumb = image;
imageThumb.Resize(65, 50);
imageThumb.Save(Path.Combine(HttpContext.Server.MapPath("/Images/News/Thumb"), fileName));
editor.Profile.ImageUrl = fileName;
return View("Index", editor.Profile);
}
示例3: Crop
public ActionResult Crop(ImageModel imageModel)
{
WebImage image = new WebImage(_PATH + imageModel.ImagePath);
var height = image.Height;
var width = image.Width;
var top = imageModel.Top;
var left = imageModel.Left;
var bottom = imageModel.Bottom;
var right = imageModel.Right;
image.Crop(top, left, height - bottom, width - right);
image.Resize(140, 200, false, false);
image.Save(_PATH + imageModel.ImagePath);
return RedirectToAction("Index");
}
示例4: Save
public ActionResult Save(string t, string l, string h, string w, string fileName)
{
try
{
// Calculate dimensions
var top = Convert.ToInt32(t.Replace("-", "").Replace("px", ""));
var left = Convert.ToInt32(l.Replace("-", "").Replace("px", ""));
var height = Convert.ToInt32(h.Replace("-", "").Replace("px", ""));
var width = Convert.ToInt32(w.Replace("-", "").Replace("px", ""));
// Get file from temporary folder
var fn = Path.Combine(Server.MapPath(MapTempFolder), Path.GetFileName(fileName));
// ...get image and resize it, ...
var img = new WebImage(fn);
img.Resize(width, height);
// ... crop the part the user selected, ...
img.Crop(top, left, img.Height - top - AvatarStoredHeight, img.Width - left - AvatarStoredWidth);
// ... delete the temporary file,...
System.IO.File.Delete(fn);
// ... and save the new one.
var newFileName = Path.GetFileName(fn);
img.Save(Server.MapPath("~/" + newFileName));
Account account = new Account(
"lifedemotivator",
"366978761796466",
"WMYLmdaTODdm4U6VcUGhxapkcjI"
);
ImageUploadResult uploadResult = new ImageUploadResult();
CloudinaryDotNet.Cloudinary cloudinary = new CloudinaryDotNet.Cloudinary(account);
var uploadParams = new ImageUploadParams()
{
File = new FileDescription(Server.MapPath("~/" + newFileName)),
PublicId = User.Identity.Name + newFileName,
};
uploadResult = cloudinary.Upload(uploadParams);
System.IO.File.Delete(Server.MapPath("~/" + newFileName));
UrlAvatar = uploadResult.Uri.ToString();
return Json(new { success = true, avatarFileLocation = UrlAvatar });
}
catch (Exception ex)
{
return Json(new { success = false, errorMessage = "Unable to upload file.\nERRORINFO: " + ex.Message });
}
}
示例5: CropImage
private WebImage CropImage(WebImage image, int width, int height)
{
var cropper = new Cropping();
double multi = cropper.GetMultiplicator(new Size(image.Width, image.Height), new Size(width, height));
double fWidth = image.Width*multi;
double fHeight = image.Height*multi;
image = image.Resize((int)fWidth, (int)fHeight,preserveAspectRatio:false );
int iWidth = image.Width;
int iHeight = image.Height;
int top = Math.Max((iHeight - height) / 2, 0);
int left = Math.Max((iWidth - width) / 2, 0);
int bottom = Math.Max(iHeight - (height + top), 0);
int right = Math.Max(iWidth - (width + left), 0);
return image.Crop(top, left, bottom, right);
}
示例6: CropImage
public JsonResult CropImage(string coords, string url)
{
var coordinates = new JavaScriptSerializer().Deserialize<Dictionary<string, int>>(coords);
var image = new WebImage(_userImagesService.GetImageStream(url));
image.FileName = "cropped." + image.ImageFormat;
image.Crop(
coordinates["y1"],
coordinates["x1"],
image.Height - coordinates["y2"],
image.Width - coordinates["x2"]);
return
Json(
_userImagesService.SaveUserImageStream(
new MemoryStream(image.GetBytes()),
image.FileName,
image.ImageFormat, image.Height, image.Width),
"text/plain");
}
示例7: Edit
public ActionResult Edit(EditorInputModel editor)
{
string fileName = editor.Profile.ImageUrl;
Console.WriteLine("test");
var image = new WebImage(HttpContext.Server.MapPath("/Images/") + fileName);
//the values to crop off.
double top = editor.Top;
double left = editor.Left;
double bottom = editor.Bottom;
double right = editor.Right;
image.Crop((int)top, (int)left, (int)bottom, (int)right);
//the image size I wanted
image.Resize(620, 280);
image.Save(Path.Combine(HttpContext.Server.MapPath("/Images/News"), fileName));
System.IO.File.Delete(Path.Combine(HttpContext.Server.MapPath("/Images/"), fileName));
editor.Profile.ImageUrl = fileName;
return View("Index", editor.Profile);
}
示例8: UploadAjax
public int UploadAjax(Stream InputStream, string fileName, bool thumbnail = true)
{
int num = -1;
using (StreamReader streamReader = new StreamReader(InputStream))
{
string[] strArray = streamReader.ReadToEnd().Split(new char[1] { ',' });
string str1 = strArray[0].Split(new char[1] { ':' })[1];
byte[] numArray = Convert.FromBase64String(strArray[1]);
if (numArray.Length > 0)
{
if (str1.Contains("image"))
{
string imageFormat = str1.Split(new char[1] { ';' })[0].Replace("image/", "");
WebImage webImage = new WebImage(numArray);
if (webImage != null)
{
string str2 = "upload_" + (object)DateTime.Now.Ticks;
webImage.Save(this._Path + str2, imageFormat, true);
if (thumbnail)
{
webImage.Resize(this._ThumbHeight, this._ThumbWidth, true, false);
webImage.Crop(1, 1, 0, 0).Save(this._ThumbnailPath + str2, imageFormat, true);
}
num = 1;
}
}
else
{
string str2 = "upload_" + (object)DateTime.Now.Ticks + new FileInfo(fileName).Extension;
TFile.StreamToFile((Stream)new MemoryStream(numArray), this._Path + str2);
num = 1;
}
}
}
return num;
}
示例9: Edit
public ActionResult Edit(EditorInputViewModel editor)
{
// cleaning directory just for demonstrations
foreach (var f in Directory.GetFiles(StorageCrop)) System.IO.File.Delete(f);
var image = new WebImage(StorageRoot + editor.Imagem);
int _height = (int)editor.Height;
int _width = (int)editor.Width;
if (_width > 0 && _height > 0)
{
image.Resize(_width, _height, false, false);
int _top = (int)(editor.Top);
int _bottom = (int)editor.Bottom;
int _left = (int)editor.Left;
int _right = (int)editor.Right;
_height = (int)image.Height;
_width = (int)image.Width;
image.Crop(_top, _left, (_height - _bottom), (_width - _right));
}
var originalFile = editor.Imagem;
var name = "profile-c" + Path.GetExtension(image.FileName);
editor.Imagem = Url.Content(StorageCrop + name);
image.Save(editor.Imagem);
System.IO.File.Delete(Url.Content(StorageRoot + originalFile));
return RedirectToAction("Index", "Home");
}
示例10: SaveSmallImageDetail
public static string SaveSmallImageDetail(WebImage image, int widthRequired = 100, int heightRequired = 100)
{
if (image != null)
{
var width = image.Width;
var height = image.Height;
var filename = Path.GetFileName(image.FileName);
if (width > height)
{
var leftRightCrop = (width - height) / 2;
image.Crop(0, leftRightCrop, 0, leftRightCrop);
}
else if (height > width)
{
var topBottomCrop = (height - width) / 2;
image.Crop(topBottomCrop, 0, topBottomCrop, 0);
}
if (image.Width > widthRequired)
{
image.Resize(widthRequired, ((heightRequired * image.Height) / image.Width));
}
var filepath = Path.Combine(DefaulDetailImg, filename);
image.Save(filepath);
return filepath.TrimStart('~');
}
return "";
}
示例11: UploadForm
public int UploadForm(HttpPostedFileBase file, bool thumbnail = true)
{
int num = -1;
if (file.ContentLength > 0)
{
if (file.ContentType.Contains("image"))
{
WebImage webImage = new WebImage(file.InputStream);
if (webImage != null)
{
string imageFormat = file.ContentType.Replace("image/", "");
string str = "upload_" + (object)DateTime.Now.Ticks;
webImage.Save(this._Path + str, imageFormat, true);
if (thumbnail)
{
webImage.Resize(this._ThumbHeight, this._ThumbWidth, true, false);
webImage.Crop(1, 1, 0, 0).Save(this._ThumbnailPath + str, imageFormat, true);
}
num = 1;
}
}
else
{
string str = "upload_" + (object)DateTime.Now.Ticks + new FileInfo(file.FileName).Extension;
TFile.StreamToFile(file.InputStream, this._Path + str);
}
}
return num;
}
示例12: SaveImageFile
private string SaveImageFile(HttpPostedFileBase file, Guid id)
{
// Define destination
string folderName = Url.Content(ConfigurationManager.AppSettings["NewsImage"]);
if (string.IsNullOrWhiteSpace(folderName)) { throw new ArgumentNullException(); }
string FileUrl = string.Format(folderName, id.ToString());
string fullFileName = HttpContext.Server.MapPath(FileUrl);
var serverPath = Path.GetDirectoryName(fullFileName);
if (Directory.Exists(serverPath) == false)
{
Directory.CreateDirectory(serverPath);
}
var img = new WebImage(file.InputStream);
int width = 1280;
int height = 750;
int minheight = 400;
double ratio = (double)img.Width / img.Height;
double desiredRatio = (double)width / height;
if (ratio > desiredRatio)
{
height = Convert.ToInt32(width / ratio);
if (height < minheight)
{
int delta = Convert.ToInt32((minheight * ratio - img.Width) / 2);
img.Crop(0, delta, 0, delta);
}
}
if (ratio < desiredRatio)
{
int delta = Convert.ToInt32((img.Height - img.Width / desiredRatio) / 2);
img.Crop(delta, 0, delta, 0);
}
img.Resize(width, height, true, true);
if (System.IO.File.Exists(fullFileName))
System.IO.File.Delete(fullFileName);
img.Save(fullFileName);
return FileUrl;
}
示例13: Crop
public ActionResult Crop(CropForm form)
{
if (form.x <= 0 && form.y <= 0 && form.x2 <= 0 && form.y2 <= 0)
{
ModelState.AddModelError("", "You must provide a selection!");
return View(form);
}
if (db.Resources.Any(x => x.Title == form.Title))
{
ModelState.AddModelError("Title", "Another Resource has this Title.");
return View(form);
}
string oldImagePath = Path.Combine(Server.MapPath("~/ResourceUploads"), form.ResourceID.ToString());
FileStream stream = new FileStream(oldImagePath, FileMode.Open);
WebImage image = new WebImage(stream);
int width = image.Width;
int height = image.Height;
image.Crop((int)Math.Ceiling(form.y), (int)Math.Ceiling(form.x), height - (int)Math.Ceiling(form.y2), width - (int)Math.Ceiling(form.x2));
//image.Crop((int)form.y, (int)form.x, (int)form.y2, (int)form.x2);
Resource newResource = new Resource
{
ID = Guid.NewGuid(),
Title = form.Title,
CreatorID = SiteAuthentication.GetUserCookie().ID,
DateAdded = DateTime.Now,
Type = form.Type,
Source = form.Source,
SourceTextColorID = form.SourceTextColorID
};
string newImagePath = Path.Combine(Server.MapPath("~/ResourceUploads"), newResource.ID.ToString());
db.Resources.Add(newResource);
image.Save(newImagePath, null, false);
db.SaveChanges();
return View("_CloseAndRefreshParent");
}
示例14: SaveImageFile
//private string GetWebsiteHtml(string url)
//{
// WebRequest request = WebRequest.Create(url);
// WebResponse response = request.GetResponse();
// Stream stream = response.GetResponseStream();
// StreamReader reader = new StreamReader(stream);
// string result = reader.ReadToEnd();
// stream.Dispose();
// reader.Dispose();
// return result;
//}
private string SaveImageFile(string imageUrl, Guid id, int num)
{
// Define destination
string folderName = Url.Content(ConfigurationManager.AppSettings["NewsImage"]);
if (string.IsNullOrWhiteSpace(folderName)) { throw new ArgumentNullException(); }
string FileUrl = num == 0 ? string.Format(folderName, id.ToString()) : string.Format(folderName, id.ToString() + "_" + num.ToString());
string fullFileName = HttpContext.Server.MapPath(FileUrl);
var serverPath = Path.GetDirectoryName(fullFileName);
if (Directory.Exists(serverPath) == false)
{
Directory.CreateDirectory(serverPath);
}
// Generate unique file name
//var fileName = id.ToString() + ".jpg"; //Path.GetFileName(file.FileName);
//fileName = SaveTemporaryAvatarFileImage(file, serverPath, fileName);
WebImage img;
try
{
System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(imageUrl);
webRequest.AllowWriteStreamBuffering = true;
webRequest.Timeout = 30000;
System.Net.WebResponse webResponse = webRequest.GetResponse();
System.IO.Stream stream = webResponse.GetResponseStream();
img = new WebImage(stream);
webResponse.Close();
}
catch (Exception ex)
{
return null;
}
int width = 1280;
int height = 750;
int minheight = 400;
double ratio = (double)img.Width / img.Height;
double desiredRatio = (double)width / height;
if (ratio > desiredRatio)
{
height = Convert.ToInt32(width / ratio);
if (height < minheight)
{
int delta = Convert.ToInt32((minheight * ratio - img.Width) / 2);
img.Crop(0, delta, 0, delta);
}
//width = Convert.ToInt32(height * ratio);
}
if (ratio < desiredRatio)
{
int delta = Convert.ToInt32((img.Height - img.Width / desiredRatio) / 2);
img.Crop(delta, 0, delta, 0);
}
img.Resize(width, height, true, true);
//img.Resize(400, (int)(400 * ratio)); // ToDo - Change the value of the width of the image oin the screen
//string fullFileName = string.Format(serverPath, id.ToString());
if (System.IO.File.Exists(fullFileName))
System.IO.File.Delete(fullFileName);
img.Save(fullFileName);
return FileUrl;
}
示例15: ResizeImageAndCropToFit
/// <summary>
/// Resizes the image and crop to fit.
/// </summary>
/// <param name="sourceStream">The source stream.</param>
/// <param name="destinationStream">The destination stream.</param>
/// <param name="size">The size.</param>
private void ResizeImageAndCropToFit(Stream sourceStream, Stream destinationStream, Size size)
{
using (var tempStream = new MemoryStream())
{
sourceStream.Seek(0, SeekOrigin.Begin);
sourceStream.CopyTo(tempStream);
var image = new WebImage(tempStream);
// Make image rectangular.
WebImage croppedImage;
var diff = (image.Width - image.Height) / 2.0;
if (diff > 0)
{
croppedImage = image.Crop(0, Convert.ToInt32(Math.Floor(diff)), 0, Convert.ToInt32(Math.Ceiling(diff)));
}
else if (diff < 0)
{
diff = Math.Abs(diff);
croppedImage = image.Crop(Convert.ToInt32(Math.Floor(diff)), 0, Convert.ToInt32(Math.Ceiling(diff)));
}
else
{
croppedImage = image;
}
var resizedImage = croppedImage.Resize(size.Width, size.Height);
var bytes = resizedImage.GetBytes();
destinationStream.Write(bytes, 0, bytes.Length);
}
}