在用户选择要上传的照片时,我需要更改照片的大小并将其保存为新的大小。
i使用此代码,但未保存为新大小。有什么问题 ?
public async Task<IActionResult> UploadNewsPic()
{
var file = Request.Form.Files[0];
try
{
if (file.Length > 0)
{
string fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
string fullPath = Path.Combine(_applicationRoot.UploadNewPath(), file.Name);
using (var stream = new FileStream(fullPath, FileMode.Create))
{
if (file.IsImage())
{
await file.ResizeImage(3, 3);
file.CopyTo(stream);
}
}
}
return Ok();
}
catch (Exception e)
{
return BadRequest();
}
}
这是调整大小范围:
public async static Task<Image> ResizeImage(this IFormFile file, int width, int height)
{
using (var memoryStream = new MemoryStream())
{
await file.CopyToAsync(memoryStream);
using (var img = Image.FromStream(memoryStream))
{
return img.Resize(width, height);
}
}
}
public static Image Resize(this Image image, int width, int height)
{
var res = new Bitmap(width, height);
using (var graphic = Graphics.FromImage(res))
{
graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphic.SmoothingMode = SmoothingMode.HighQuality;
graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphic.CompositingQuality = CompositingQuality.HighQuality;
graphic.DrawImage(image, 0, 0, width, height);
}
return res;
}
答案 0 :(得分:2)
您的ResizeImage()
函数将返回一个已调整大小的图像,它不会编辑图像本身,因此必须将其设置为变量。
if (file.IsImage())
{
Image imageResized = await file.ResizeImage(3, 3);
// ...
}
答案 1 :(得分:2)
您需要将let fetchRequest = NSFetchRequest<Project>(entityName: "Project")
let sortDescriptor = NSSortDescriptor(key: "pname", ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]
do {
let newProjectArray = try managedContext.fetch(fetchRequest)
}catch let error as NSError {
print("Could not fetch. \(error), \(error.userInfo)")
}
中的结果保存到流中。现在,您只是在复制原始文件。
func numberOfSections(in tableView: UITableView) -> Int {
return (newProjectArray != nil) ? newProjectArray!.count : 0
}
func tableView(_ tableView: UITableView, numberOfRowsInSection
section: Int) -> Int {
if newProjectArray![section].isExpandable, let folders = newProjectArray![section].folders {
return folders.count
}
else{
return 0
}
}
func tableView(_ tableView: UITableView, heightForHeaderInSection
section: Int) -> CGFloat {
return 40
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath:
IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier:
"FoldersCell") as! FoldersCell
if newProjectArray![indexPath.section].isExpandable == true {
if let folders = newProjectArray![indexPath.section].folders {
folder = folders[indexPath.row]
cell.projectNameLabel.text = folder.name
}
}
return cell
}