跳到主要內容

[ASP.NET] 上傳圖片加浮水印

Model [.CS]

        [HttpPost]
        public int WebUplaodFile()
        {
            var upfile = Request.Files["myFile"]; ;
            if (upfile != null)
            {
                new ImgService().ImgMarkImg(Server.MapPath("~/Image"), upfile);
            }
         
            return 1;
        }


Service [.CS]

        public void ImgMarkImg(string spath, HttpPostedFileBase myfile)
        {
            //浮水印檔案路徑
            string MarkImgPath = spath + "/imgmark.png";
            //暫存檔路徑
            string tmpPath = spath + "/Temp/tmp" + DateTime.Now.ToString("yyyyMMddHHmmssff") + ".jpg";
            myfile.SaveAs(tmpPath);

            //作為浮水印的圖檔
            System.Drawing.Image watermarkImage = System.Drawing.Image.FromFile(MarkImgPath);
            //加浮水印
            System.Drawing.Image image = System.Drawing.Image.FromFile(tmpPath);
            ImageFormat thisFormat = image.RawFormat;

            //縮圖
            int fixWidth = 0;
            int fixHeight = 0;
            int maxPx = 700; //可依浮水印大小作上限依據
            if (image.Width > maxPx || image.Height > maxPx)
            {
                if (image.Width >= image.Height)
                {
                    fixWidth = maxPx;
                    fixHeight = Convert.ToInt32((Convert.ToDouble(fixWidth) / Convert.ToDouble(image.Width)) * Convert.ToDouble(image.Height));
                }
                else
                {
                    fixHeight = maxPx;
                    fixWidth = Convert.ToInt32((Convert.ToDouble(fixHeight) / Convert.ToDouble(image.Height)) * Convert.ToDouble(image.Width));
                }
            }
            else
            {
                fixHeight = image.Height;
                fixWidth = image.Width;
            }
            //匯出縮圖
            Bitmap imageOutput = new Bitmap(image, fixWidth, fixHeight);
            //取得畫布
            Graphics gra = Graphics.FromImage(imageOutput);
            //重新繪製縮圖並加浮水印
            gra.DrawImage(watermarkImage, new Rectangle((imageOutput.Width - watermarkImage.Width) / 2, (imageOutput.Height - watermarkImage.Height) / 2, imageOutput.Width, image.Height), 0, 0, imageOutput.Width, imageOutput.Height, GraphicsUnit.Pixel);
       
            imageOutput.Save(spath + "/Img/" + DateTime.Now.ToString("yyyyMMdd") + ".jpg", thisFormat);
            imageOutput.Dispose();
            image.Dispose();
            gra.Dispose();

            //刪除暫存
            File.Delete(tmpPath);

        }

留言

這個網誌中的熱門文章

[python] python 時區轉換 ( timezone )

python 時區轉換 import datetime as dt import pytz d = dt.datetime(2019,1,1,14,0,0) #create Taipei timezone tw = pytz.timezone('Asia/Taipei') #set d timezone is 'Asia/Taipei' twdt = tw.localize(d) #change to utc time utc_dt = twdt.astimezone(pytz.utc) *注意 網路上很多使用 d.replace(tzinfo=tw)   但是可能會出現時差問題, 如下: datetime.time(23, 18, 5, tzinfo=<DstTzInfo 'Asia/Shanghai' LMT+8:06:00 STD>) 參考文件 python时区设置——pytz模块

[HTML] 文字浮水印效果 text watermark css

[.css] .info-mask {     height: 0;       position: absolute;       margin-top:120px;   } .info-opacity {     opacity: 0.8;     color: black; } .info-text {     color: gray;     font-size: 40px;     -webkit-transform: rotate(-5deg);       line-height: 60px;     max-width:1200px;     letter-spacing:3px;     padding-left:0.8em; } [.html]     <div class="info-mask">         <p class="info-text">             WaterMarkText         </p>     </div>    <table class="table table-bordered table-striped table-responsive table-hover" style="position:                 relative;" >        <tbody class="info-opacity" >        </tbody>    </table>

[GCP] Google Cloud Platform 搬移 VM & cloud sql & datastore

建立同VM 同專案 VM  建立快照 --> 用快照建立新的VM 跨專案 VM  建立快照  --> 用快照建立映像檔 --> 用印象檔建立新的VM Notice  開機磁碟: 自訂映像檔->選擇印象檔 搬移cloud sql step 1. A SQL [Top option] '匯出' setting Bucket and formate is 'SQL' step 2. B SQL [Top option] '匯入' setting Bucket and formate is 'SQL' Bucket path: gs://{Bucket}/file.sql Notice   不同專案的gs要有權限讀取 搬移 datastore 詳細請參考: https://cloud.google.com/datastore/docs/export-import-entities   step 1. 'new' project [menu] 資料儲存庫 -> 啟用datastore step 2. gcloud terminal gcloud auth login  gcloud config set project <projectid> gcloud beta datastore export --kinds=" <KIND> " gs:// <BUCKET> gcloud config set project new-projectid gcloud datastore export gs:// <BUCKET> Notice  跨專案gs storage auth 需要新增  xxx@appspot.gserviceaccount.com  [storage BUCKET 讀取者] 權限