再贴一个上传图片加水印的代码

public string GetRandom()
  {
   Random r=new Random();
   return r.Next(10000).ToString();
  }

  private void upImge()
  {
   string fileName=Path.GetFileName(File1.PostedFile.FileName);
   string txt=fileName.Substring(fileName.LastIndexOf(".")+1);
   if(fileName!="")
   {    
    string time=DateTime.Now.Month.ToString()+DateTime.Now.Hour.ToString()+DateTime.Now.Minute.ToString()+DateTime.Now.Second.ToString()+DateTime.Now.Millisecond.ToString();
    string newFileName=time+this.GetRandom()+"."+txt;

    string filepath = Server.MapPath("upPic/") + newFileName;
    if (File.Exists(filepath))
    {
     js.showAlert("该文件名已存在!");
    }      
    else
    {
     if(File1.PostedFile != null)
     {
       File1.PostedFile.SaveAs(filepath);
       System.Drawing.Image image = System.Drawing.Image.FromFile(filepath);
       double h=Convert.ToDouble(image.Height.ToString());
       double w=Convert.ToDouble(image.Width.ToString());
       double f=h/w;
       if(h>w)
       {
        if(h>150)
        {
         h=150;
         w=150*(1/f);
        }
        else
        {
         h=150;
        }
       }
       else if(w>h)
       {
        if(w>150)
        {
         w=150;
         h=150*f;
        }
        else
        {
         w=150;
        }
       }
       else
       {
        h=150;
        w=150;
       }
       image=image.GetThumbnailImage(Convert.ToInt32(w),Convert.ToInt32(h),null,new System.IntPtr());
       Graphics g = Graphics.FromImage(image);
       g.DrawImage(image, 0, 0, image.Width, image.Height);
       Font f11 = new Font("Verdana", 5);
       Brush b = new SolidBrush(Color.White);
       g.DrawString("传媒中国", f11, b, 10, 10);
       g.Dispose();

       string newPath = Server.MapPath("upPic/") + "_new" +newFileName;
       image.Save(newPath);
       image.Dispose();

       js.showAlert("上传成功");      
     }
     else
     {
      js.showAlert("图片为空!");
     }
    }
   }
   else
   {
    js.showAlert("请选择一个文件!");
   }
  } 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个使用Java编写的为上传到Minio的照片文件添加水印的示例代码: ```java import io.minio.MinioClient; import io.minio.errors.MinioException; import org.apache.commons.io.IOUtils; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.text.SimpleDateFormat; import java.util.Date; public class MinioUploadDemo { public static void main(String[] args) { try { // 初始化MinioClient MinioClient minioClient = new MinioClient("http://minio-server:9000", "minio-access-key", "minio-secret-key"); // 上传文件 String bucketName = "test-bucket"; String objectName = "example.jpg"; String filePath = "path/to/local/file.jpg"; InputStream inputStream = new FileInputStream(new File(filePath)); String contentType = "image/jpeg"; minioClient.putObject(bucketName, objectName, inputStream, contentType); // 给上传的图片添加水印 addWatermark(minioClient, bucketName, objectName); } catch (Exception e) { System.out.println("Error occurred: " + e); } } private static void addWatermark(MinioClient minioClient, String bucketName, String objectName) throws Exception { // 下载图片 InputStream inputStream = minioClient.getObject(bucketName, objectName); // 读取图片 BufferedImage image = ImageIO.read(inputStream); // 添加水印 Graphics2D graphics = image.createGraphics(); graphics.setColor(Color.RED); graphics.setFont(new Font("宋体", Font.BOLD, 30)); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String watermark = "水印:" + sdf.format(new Date()); graphics.drawString(watermark, 0, image.getHeight() - 30); graphics.dispose(); // 上传图片 InputStream newInputStream = IOUtils.toInputStream(image.toString(), "UTF-8"); minioClient.putObject(bucketName, objectName, newInputStream, "image/jpeg"); } } ``` 这段代码使用了 `minio-java` 库连接到Minio服务器,并且在上传文件到Minio之后,调用了 `addWatermark()` 方法,该方法会从Minio服务器上下载刚才上传的照片,然后添加水印并再次上传到Minio服务器。添加水印的方法使用了Java2D API来实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值