When using the method in task mode it gives a parameter error in the line image: gr.DrawImage (this.image, 0, 0, this.width, this.height);
how can I arrange this in a way that works.Where am I going wrong? This is my abstract class that works like this:
using System;
using System.Reflection;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Threading.Tasks;
namespace Class
{
public abstract class Img{
protected Image image {get;set;}
protected Image thumb {get;set;}
protected int width {get;set;}
protected int height {get;set;}
protected string path {get;set;}
protected string extensao {get;set;}
protected string pathNew {get;set;}
protected int percent {get;set;}
protected int maxWidth {get;set;}
protected int maxHeight {get;set;}
public T GetPrivateProperty<T>(object obj, string propertyName)=> (T) obj.GetType()
.GetProperty(propertyName, BindingFlags.Instance | BindingFlags.NonPublic)
.GetValue(obj);
public void SetPrivateProperty<T>(object obj, string propertyName, T value)=>
obj.GetType()
.GetProperty(propertyName, BindingFlags.Instance | BindingFlags.NonPublic)
.SetValue(obj, value);
public void ResizePercent(){
Console.WriteLine("Entrou");
Task task = new Task(()=>this.GetImage());
task.Start();
task.Wait();
this.width = Convert.ToInt32(this.width * this.percent / 100);
this.height =Convert.ToInt32(this.height * this.percent / 100);
this.SetBody();
}
public void ResizeMax(){
///this.image = await Task.Run(() => this.GetImage());
var ratioX = (double)this.maxWidth / this.image.Width;
var ratioY = (double)this.maxHeight / this.image.Height;
var ratio = Math.Min(ratioX, ratioY);
var newWidth = (int)(this.image.Width * ratio);
var newHeight = (int)(this.image.Height * ratio);
this.SetBody();
}
public void SetBody(){
// Create a Bitmap object
Bitmap myBitmap = new Bitmap(this.width,this.height);
Console.WriteLine($"{this.width},{this.height},{this.image},{this.extensao},{myBitmap}");
using(Graphics gr = Graphics.FromImage(myBitmap)){
gr.SmoothingMode = SmoothingMode.HighQuality;
gr.SmoothingMode = SmoothingMode.AntiAlias;
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
// Draw myBitmap to the screen.
gr.DrawImage(this.image, 0, 0,this.width,this.height);
if(this.extensao=="GIF" || this.extensao=="PNG"){
// Get the color of a background pixel.
Color backColor = myBitmap.GetPixel(1, 1);
// Make backColor transparent for myBitmap.
myBitmap.MakeTransparent(backColor);
// Draw the transparent bitmap to the screen.
gr.DrawImage(myBitmap,0, 0,this.width,this.height);
}
myBitmap.Save($"{this.pathNew}/{Path.ChangeExtension(Path.GetRandomFileName(),this.extensao)}");
}
}
public void GetImage(){
Console.WriteLine("entrou GetImage");
using (MemoryStream ms = new MemoryStream(File.ReadAllBytes(this.path))){
using (Image img = Image.FromStream(ms)){
this.image=img;
this.extensao = this.image.RawFormat.ToString().ToUpper();
this.width = this.image.Width;
this.height = this.image.Height;
Console.WriteLine(this.image);
}
}
}
}
}
This is the method that generates the image but when I capture this.image in the setBody () method it gives "Parameter is not valid."
public void GetImage(){
Console.WriteLine("entrou GetImage");
using (MemoryStream ms = new MemoryStream(File.ReadAllBytes(this.path))){
using (Image img = Image.FromStream(ms)){
this.image=img;
this.extensao = this.image.RawFormat.ToString().ToUpper();
this.width = this.image.Width;
this.height = this.image.Height;
Console.WriteLine(this.image);
}
}
}
Line error:
public void SetBody(){
// Create a Bitmap object
Bitmap myBitmap = new Bitmap(this.width,this.height);
Console.WriteLine($"{this.width},{this.height},{this.image},{this.extensao},{myBitmap}");
using(Graphics gr = Graphics.FromImage(myBitmap)){
gr.SmoothingMode = SmoothingMode.HighQuality;
gr.SmoothingMode = SmoothingMode.AntiAlias;
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
// Line error:
gr.DrawImage(this.image, 0, 0,this.width,this.height);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…