C# 如何将超过图像值的百分比值放入水印中
我有两个图像,一个水印和另一个将接收标记。例如,我想在水印中加入一个百分比值,该值将超过图像的值 我的代码:C# 如何将超过图像值的百分比值放入水印中,c#,algorithm,C#,Algorithm,我有两个图像,一个水印和另一个将接收标记。例如,我想在水印中加入一个百分比值,该值将超过图像的值 我的代码: public void SetBodyWater(){ this.gr = Graphics.FromImage(this.image); float w = this.image.Width*this.percent/100; float h = this.image.Height*this.percent/100; if(thi
public void SetBodyWater(){
this.gr = Graphics.FromImage(this.image);
float w = this.image.Width*this.percent/100;
float h = this.image.Height*this.percent/100;
if(this.waterMark.Width>this.image.Width){
w = w/this.waterMark.Width;
w = w*this.waterMark.Width;
this.width = Convert.ToInt32(w);
}else{
this.width =this.waterMark.Width;
}
if(this.waterMark.Height>this.image.Height){
h = h/this.waterMark.Height;
h = h*this.waterMark.Height;
this.height = Convert.ToInt32(h);
}else{
this.height = this.waterMark.Height;
}
int x = Convert.ToInt32(this.image.Width / 2 - this.width / 2);
int y = Convert.ToInt32(this.image.Height / 2 - this.height / 2);
Console.Write($"{this.width},{this.height}");
this.gr.DrawImage(this.waterMark, x, y, this.width, this.height);
this.GetFormat();
this.image.Save($"{this.pathNew}/{Path.ChangeExtension(Path.GetRandomFileName(), this.extensao.ToLower())}",
this.format);
}
但它不起作用。我的解决方案:
public void SetBodyWater(){
this.gr = Graphics.FromImage(this.image);
if(this.waterMark.Height<this.image.Height && this.waterMark.Width<this.image.Width){
this.width = Convert.ToInt32(this.waterMark.Width*this.percent/100);
this.height = Convert.ToInt32(this.waterMark.Height*this.percent/100);
}else{
// float w = (float)this.image.Width*this.percent/100;
// float h = (float)this.image.Height*this.percent/100;
float w = (float)this.image.Width/this.waterMark.Width;
float h = (float)this.image.Height/this.waterMark.Height;
float maxW = Math.Min(w,h);
float maxH = Math.Min(w,h);
if(this.waterMark.Width>this.image.Width){
maxW =(float) maxW*this.waterMark.Width;
maxW = (float)maxW*this.percent/100;
this.width = Convert.ToInt32(maxW);
// w = (float) w/this.waterMark.Width;
// w =(float) w*this.waterMark.Width;
// this.width = Convert.ToInt32(w);
}else{
float hs = (float)this.image.Height*this.percent/100;
h = (float) h/this.waterMark.Height;
h = (float) h*this.waterMark.Height;
this.height = Convert.ToInt32(h);
float p =(float) this.height/this.waterMark.Height;
this.width = Convert.ToInt32(p*this.waterMark.Width);
}
if(this.waterMark.Height>this.image.Height){
maxH =(float) maxH*this.waterMark.Height;
maxH = (float)maxH*this.percent/100;
this.height = Convert.ToInt32(maxH);
// h = (float) h/this.waterMark.Height;
// h = (float) h*this.waterMark.Height;
// this.height = Convert.ToInt32(h);
}else{
float p =(float) this.width/this.waterMark.Width;
this.height = Convert.ToInt32(p*this.waterMark.Height);
}
}
// int x = Convert.ToInt32(this.image.Width / 2 - this.waterMark.Width / 2);
// int y = Convert.ToInt32(this.image.Height / 2 - this.waterMark.Height / 2);
int x = Convert.ToInt32(this.image.Width / 2 - this.width / 2);
int y = Convert.ToInt32(this.image.Height / 2 - this.height / 2);
Console.Write($"{this.width},{this.height}");
//this.gr.ScaleTransform(sacle11,sacle12);
this.gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
this.gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
this.gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
this.gr.DrawImage(this.waterMark, x, y,this.width,this.height);
this.GetFormat();
this.pathSucess = $"{this.pathNew}/{Path.ChangeExtension(Path.GetRandomFileName(), this.extensao.ToLower())}";
this.image.Save(this.pathSucess,this.format);
}
public void SetBodyWater(){
this.gr=Graphics.FromImage(this.image);
if(this.waterMark.Heightthis.image.Height){
maxH=(float)maxH*this.waterMark.Height;
maxH=(float)maxH*this.percent/100;
this.height=Convert.ToInt32(最大值);
//h=(浮动)h/this.waterMark.Height;
//h=(float)h*this.waterMark.Height;
//this.height=转换为32(h);
}否则{
float p=(float)this.width/this.waterMark.width;
this.height=Convert.ToInt32(p*this.waterMark.height);
}
}
//int x=Convert.ToInt32(this.image.Width/2-this.waterMark.Width/2);
//int y=转换.ToInt32(this.image.Height/2-this.waterMark.Height/2);
int x=Convert.ToInt32(this.image.Width/2-this.Width/2);
int y=转换为32(this.image.Height/2-this.Height/2);
Write($“{this.width},{this.height}”);
//这个.gr.ScaleTransform(sacle11,sacle12);
this.gr.CompositingQuality=System.Drawing.Drawing2D.CompositingQuality.HighQuality;
this.gr.InterpolationMode=System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
this.gr.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.HighQuality;
this.gr.DrawImage(this.waterMark,x,y,this.width,this.height);
这个是.GetFormat();
this.pathSucess=$“{this.pathNew}/{Path.ChangeExtension(Path.GetRandomFileName(),this.extensao.ToLower())}”;
this.image.Save(this.pathSucess,this.format);
}
我的班级:
using System;
using System.Reflection;
using System.Drawing;
using System.Drawing.Imaging;
//using System.Drawing.Drawing2D;
using System.IO;
//using System.Threading.Tasks;
namespace Class
{
public abstract class Img : IDisposable{
private bool _disposed = false;
protected Image image {get;set;}
protected Image waterMark{get;set;}
protected int width {get;set;}
protected int height {get;set;}
protected string path {get;set;}
protected string pathWater{get;set;}
protected string extensao {get;set;}
protected string pathNew {get;set;}
protected string pathSucess {get;set;}
protected int percent {get;set;}
protected int maxWidth {get;set;}
protected int maxHeight {get;set;}
protected ImageFormat format{get;set;}
protected Graphics gr{get;set;}
protected Color backColor{get;set;}
protected Bitmap myBitmap {get;set;}
protected MemoryStream ms{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 GetFormat(){
switch (this.extensao){
case "GIF":
this.format = ImageFormat.Gif;
break;
case "PNG":
this.format = ImageFormat.Png;
break;
case "JPEG":
this.format = ImageFormat.Jpeg;
break;
default:
break;
}
}
public void SetTransparent(){
// Get the color of a background pixel.
this.backColor = this.myBitmap.GetPixel(0, 0);
// Make backColor transparent for myBitmap.
this.myBitmap.MakeTransparent(this.backColor);
// Draw the transparent bitmap to the screen.
this.gr.DrawImage(this.myBitmap,0, 0,this.width,this.height);
}
public void GetImage(){
this.ms = new MemoryStream(File.ReadAllBytes(this.path));
this.image = Image.FromStream(this.ms);
this.extensao = this.image.RawFormat.ToString().ToUpper();
}
public void GetWaterMark(){
this.ms = new MemoryStream(File.ReadAllBytes(this.pathWater));
this.waterMark = Image.FromStream(ms);
}
public void Dispose() => Dispose(true);
protected virtual void Dispose(bool disposing){
if (_disposed){
return;
}
if (disposing){
// Dispose managed state (managed objects).
this.gr?.Dispose();
this.image?.Dispose();
this.waterMark?.Dispose();
this.ms?.Dispose();
}
_disposed = true;
}
}
}
使用系统;
运用系统反思;
使用系统图;
使用系统、绘图、成像;
//使用System.Drawing.Drawing2D;
使用System.IO;
//使用System.Threading.Tasks;
命名空间类
{
公共抽象类Img:IDisposable{
private bool_disposed=false;
受保护的图像{get;set;}
受保护的图像水印{get;set;}
受保护的整数宽度{get;set;}
受保护的整数高度{get;set;}
受保护的字符串路径{get;set;}
受保护的字符串路径{get;set;}
受保护的字符串extensao{get;set;}
受保护的字符串pathNew{get;set;}
受保护的字符串路径成功{get;set;}
受保护的整数百分比{get;set;}
受保护的整型最大宽度{get;set;}
受保护的整数最大高度{get;set;}
受保护的ImageFormat格式{get;set;}
受保护图形gr{get;set;}
保护色背景色{get;set;}
受保护位图myBitmap{get;set;}
受保护内存流ms{get;set;}
public T GetPrivateProperty(对象对象obj,字符串propertyName)=>(T)obj.GetType()
.GetProperty(propertyName,BindingFlags.Instance | BindingFlags.NonPublic)
.GetValue(obj);
public void SetPrivateProperty(对象对象对象,字符串属性名称,T值)=>
obj.GetType()
.GetProperty(propertyName,BindingFlags.Instance | BindingFlags.NonPublic)
.设定值(obj,值);
public void GetFormat(){
开关(此.extensao){
案例“GIF”:
this.format=ImageFormat.Gif;
打破
案例“PNG”:
this.format=ImageFormat.Png;
打破
案例“JPEG”:
this.format=ImageFormat.Jpeg;
打破
违约:
打破
}
}
公共空间{
//获取背景像素的颜色。
this.backColor=this.myBitmap.GetPixel(0,0);
//使myBitmap的背景色透明。
this.myBitmap.MakeTransparent(this.backColor);
//将透明位图绘制到屏幕上。
this.gr.DrawImage(this.myBitmap,0,0,this.width,this.height);
}
public void GetImage(){
this.ms=newmemoryStream(File.ReadAllBytes(this.path));
this.image=image.FromStream(this.ms);
this.extensao=this.image.RawFormat.ToString().ToUpper();
}
public void GetWaterMark(){
this.ms=newmemorystream(File.ReadAllBytes(this.pathWater));
this.waterMark=Image.FromStream(ms);
}
public void Dispose()=>Dispose(true);
受保护的虚拟void Dispose(bool disposing){
如果(_){
返回;
}
如果(处置){
//处置托管状态(托管对象)。
这个.gr?.Dispose();
这个.image?.Dispose();
此.waterMark?.Dispose();
这个.ms?.Dispose();
}
_这是真的;
}
}
}
拇指类:
using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
//using System.Threading.Tasks;
namespace Class{
class Thumb:Class.Img{
public void ResizeMax(){
this.GetImage();
var ratioX = (double)this.maxWidth / this.image.Width;
var ratioY = (double)this.maxHeight / this.image.Height;
var ratio = Math.Min(ratioX, ratioY);
this.width = Convert.ToInt32(this.image.Width * ratio);
this.height =Convert.ToInt32(this.image.Height * ratio);
this.SetBody();
}
public void SetBody(){
// Create a Bitmap object
this.myBitmap = new Bitmap(this.width,this.height);
this.gr = Graphics.FromImage(myBitmap);
this.gr.SmoothingMode = SmoothingMode.AntiAlias;
this.gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
this.gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
// Draw myBitmap to the screen.
this.gr.DrawImage(this.image, 0, 0,this.width,this.height);
switch (this.extensao){
case "GIF":
this.SetTransparent();
this.format = ImageFormat.Gif;
break;
case "PNG":
this.SetTransparent();
this.format = ImageFormat.Png;
break;
case "JPEG":
this.format = ImageFormat.Jpeg;
break;
default:
break;
}
this.pathSucess = $"{this.pathNew}/{Path.ChangeExtension(Path.GetRandomFileName(), this.extensao.ToLower())}";
this.myBitmap.Save(this.pathSucess,this.format);
}
public void ResizePercent(){
// Task task = new Task(()=>this.GetImage());
// task.Start();
// task.Wait();
this.GetImage();
this.width = Convert.ToInt32(this.image.Width * this.percent / 100);
this.height =Convert.ToInt32(this.image.Height * this.percent / 100);
this.SetBody();
}
public void SetBodyWater(){
this.gr = Graphics.FromImage(this.image);
if(this.waterMark.Height<this.image.Height && this.waterMark.Width<this.image.Width){
this.width = Convert.ToInt32(this.waterMark.Width*this.percent/100);
this.height = Convert.ToInt32(this.waterMark.Height*this.percent/100);
}else{
// float w = (float)this.image.Width*this.percent/100;
// float h = (float)this.image.Height*this.percent/100;
float w = (float)this.image.Width/this.waterMark.Width;
float h = (float)this.image.Height/this.waterMark.Height;
float maxW = Math.Min(w,h);
float maxH = Math.Min(w,h);
if(this.waterMark.Width>this.image.Width){
maxW =(float) maxW*this.waterMark.Width;
maxW = (float)maxW*this.percent/100;
this.width = Convert.ToInt32(maxW);
// w = (float) w/this.waterMark.Width;
// w =(float) w*this.waterMark.Width;
// this.width = Convert.ToInt32(w);
}else{
float hs = (float)this.image.Height*this.percent/100;
h = (float) h/this.waterMark.Height;
h = (float) h*this.waterMark.Height;
this.height = Convert.ToInt32(h);
float p =(float) this.height/this.waterMark.Height;
this.width = Convert.ToInt32(p*this.waterMark.Width);
}
if(this.waterMark.Height>this.image.Height){
maxH =(float) maxH*this.waterMark.Height;
maxH = (float)maxH*this.percent/100;
this.height = Convert.ToInt32(maxH);
// h = (float) h/this.waterMark.Height;
// h = (float) h*this.waterMark.Height;
// this.height = Convert.ToInt32(h);
}else{
float p =(float) this.width/this.waterMark.Width;
this.height = Convert.ToInt32(p*this.waterMark.Height);
}
}
// int x = Convert.ToInt32(this.image.Width / 2 - this.waterMark.Width / 2);
// int y = Convert.ToInt32(this.image.Height / 2 - this.waterMark.Height / 2);
int x = Convert.ToInt32(this.image.Width / 2 - this.width / 2);
int y = Convert.ToInt32(this.image.Height / 2 - this.height / 2);
Console.Write($"{this.width},{this.height}");
//this.gr.ScaleTransform(sacle11,sacle12);
this.gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
this.gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
this.gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
this.gr.DrawImage(this.waterMark, x, y,this.width,this.height);
this.GetFormat();
this.pathSucess = $"{this.pathNew}/{Path.ChangeExtension(Path.GetRandomFileName(), this.extensao.ToLower())}";
this.image.Save(this.pathSucess,this.format);
}
public void ResizePercentWater(){
// Task task = new Task(()=>this.GetImage());
// task.Start();
// task.Wait();
this.GetImage();
this.GetWaterMark();
this.SetBodyWater();
}
}
}
使用系统;
使用System.IO;
使用系统图;
使用系统、绘图、成像;
使用System.Drawing.Drawing2D;
//使用System.Threading.Tasks;
命名空间类{
类拇指:class.Img{
公共空间ResizeMax(){
这是GetImage();
var ratioX=(双精度)this.maxWidth/this.image.Width;
var ratioY=(双)this.maxHeight/this.image.Height;
var比率=数学最小值(ratioX,ratioY);
this.width=Convert.ToInt32(this.image.width*比率);
this.height=Convert.ToInt32(this.image.height*比率);
这个.SetBody();
}