實作上跨 webmethod 常常不能取得 System.Web.HttpContext.Current.Session 內的資料
例如在 webmethod1 寫入然後在 webmethod2 讀取時會是null
改用 System.Web.HttpContext.Current.Cache 則ok
2019年9月25日 星期三
2019年9月24日 星期二
合併檔案(tif格式)
安裝套件: FreeSpire.Office
using Spire.Doc;
using Spire.Pdf;
string randomTempFileName = System.IO.Path.GetTempFileName();
Bitmap bmp = default, bmpclone = default;
Graphics gr = default;
try
{
List<System.Drawing.Image> images=new List<System.Drawing.Image>();
foreach (var file in files)
{
var ext = System.IO.Path.GetExtension(file.name).ToLower();
byte[] bytes =Convert.FromBase64String(file.value.Split(',')[1]);
Document doc = default;
using (MemoryStream ms = new MemoryStream(bytes))
using (MemoryStream ms1 = new MemoryStream())
switch (ext)
{
case ".doc":
doc = new Document(ms, Spire.Doc.FileFormat.Doc);
doc.SaveToStream(ms1, Spire.Doc.FileFormat.PDF);
images.AddRange(SaveAsImage(new PdfDocument(ms1)));
break;
case ".docx":
doc = new Document(ms, Spire.Doc.FileFormat.Docx2010);
doc.SaveToStream(ms1, Spire.Doc.FileFormat.PDF);
images.AddRange(SaveAsImage(new PdfDocument(ms1)));
break;
case ".pdf":
images.AddRange(SaveAsImage(new PdfDocument(ms)));
break;
case ".png":
case ".jpg":
bmp = (Bitmap)Bitmap.FromStream(ms);
bmpclone = new Bitmap(bmp.Width, bmp.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); // 配合 PdfDocument 轉為 image 的 PixelFormat 轉換成相同的
gr = Graphics.FromImage(bmpclone);
gr.DrawImage(bmp, new Rectangle(0, 0, bmpclone.Width, bmpclone.Height));
images.Add((Image)bmpclone);
break;
}
}
JoinTiffImages(images.ToArray(), randomTempFileName, EncoderValue.CompressionLZW);
}
catch (Exception ex)
{
return 回傳錯誤(ex);
}
finally
{
if (bmp != default) bmp.Dispose();
if (bmpclone != default) bmpclone.Dispose();
if (gr != default) gr.Dispose();
}
Image[] SaveAsImage(PdfDocument document)
{
if (document.Pages.Count > 3) throw new Exception("無法處理超過3頁的檔案");
Image[] images = new Image[document.Pages.Count];
for (int i = 0; i < document.Pages.Count; i++)
images[i] = document.SaveAsImage(i);
return images;
}
void JoinTiffImages(Image[] images, string outFile, EncoderValue compressEncoder)
{
//use the save encoder
Encoder enc = Encoder.SaveFlag;
EncoderParameters ep = new EncoderParameters(2);
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame);
ep.Param[1] = new EncoderParameter(Encoder.Compression, (long)compressEncoder);
Image pages = images[0];
int frame = 0;
ImageCodecInfo info = GetEncoderInfo("image/tiff");
foreach (Image img in images)
{
if (frame == 0)
{
pages = img;
//save the first frame
pages.Save(outFile, info, ep);
}
else
{
//save the intermediate frames
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);
pages.SaveAdd(img, ep);
}
if (frame == images.Length - 1)
{
//flush and close.
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);
pages.SaveAdd(ep);
}
frame++;
}
ImageCodecInfo GetEncoderInfo(string mimeType)
{
ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
for (int j = 0; j < encoders.Length; j++)
{
if (encoders[j].MimeType == mimeType)
return encoders[j];
}
throw new Exception(mimeType + " mime type not found in ImageCodecInfo");
}
}
using Spire.Doc;
using Spire.Pdf;
string randomTempFileName = System.IO.Path.GetTempFileName();
Bitmap bmp = default, bmpclone = default;
Graphics gr = default;
try
{
List<System.Drawing.Image> images=new List<System.Drawing.Image>();
foreach (var file in files)
{
var ext = System.IO.Path.GetExtension(file.name).ToLower();
byte[] bytes =Convert.FromBase64String(file.value.Split(',')[1]);
Document doc = default;
using (MemoryStream ms = new MemoryStream(bytes))
using (MemoryStream ms1 = new MemoryStream())
switch (ext)
{
case ".doc":
doc = new Document(ms, Spire.Doc.FileFormat.Doc);
doc.SaveToStream(ms1, Spire.Doc.FileFormat.PDF);
images.AddRange(SaveAsImage(new PdfDocument(ms1)));
break;
case ".docx":
doc = new Document(ms, Spire.Doc.FileFormat.Docx2010);
doc.SaveToStream(ms1, Spire.Doc.FileFormat.PDF);
images.AddRange(SaveAsImage(new PdfDocument(ms1)));
break;
case ".pdf":
images.AddRange(SaveAsImage(new PdfDocument(ms)));
break;
case ".png":
case ".jpg":
bmp = (Bitmap)Bitmap.FromStream(ms);
bmpclone = new Bitmap(bmp.Width, bmp.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); // 配合 PdfDocument 轉為 image 的 PixelFormat 轉換成相同的
gr = Graphics.FromImage(bmpclone);
gr.DrawImage(bmp, new Rectangle(0, 0, bmpclone.Width, bmpclone.Height));
images.Add((Image)bmpclone);
break;
}
}
JoinTiffImages(images.ToArray(), randomTempFileName, EncoderValue.CompressionLZW);
}
catch (Exception ex)
{
return 回傳錯誤(ex);
}
finally
{
if (bmp != default) bmp.Dispose();
if (bmpclone != default) bmpclone.Dispose();
if (gr != default) gr.Dispose();
}
Image[] SaveAsImage(PdfDocument document)
{
if (document.Pages.Count > 3) throw new Exception("無法處理超過3頁的檔案");
Image[] images = new Image[document.Pages.Count];
for (int i = 0; i < document.Pages.Count; i++)
images[i] = document.SaveAsImage(i);
return images;
}
void JoinTiffImages(Image[] images, string outFile, EncoderValue compressEncoder)
{
//use the save encoder
Encoder enc = Encoder.SaveFlag;
EncoderParameters ep = new EncoderParameters(2);
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame);
ep.Param[1] = new EncoderParameter(Encoder.Compression, (long)compressEncoder);
Image pages = images[0];
int frame = 0;
ImageCodecInfo info = GetEncoderInfo("image/tiff");
foreach (Image img in images)
{
if (frame == 0)
{
pages = img;
//save the first frame
pages.Save(outFile, info, ep);
}
else
{
//save the intermediate frames
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);
pages.SaveAdd(img, ep);
}
if (frame == images.Length - 1)
{
//flush and close.
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);
pages.SaveAdd(ep);
}
frame++;
}
ImageCodecInfo GetEncoderInfo(string mimeType)
{
ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
for (int j = 0; j < encoders.Length; j++)
{
if (encoders[j].MimeType == mimeType)
return encoders[j];
}
throw new Exception(mimeType + " mime type not found in ImageCodecInfo");
}
}
2019年9月17日 星期二
組合 image 陣列為 tif 檔案
List<System.Drawing.Image> images=new List<System.Drawing.Image>();
...
JoinTiffImages(images.ToArray(), 檔名, EncoderValue.CompressionLZW);
void JoinTiffImages(Image[] images, string outFile, EncoderValue compressEncoder)
{
//use the save encoder
Encoder enc = Encoder.SaveFlag;
EncoderParameters ep = new EncoderParameters(2);
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame);
ep.Param[1] = new EncoderParameter(Encoder.Compression, (long)compressEncoder);
Image pages = images[0];
int frame = 0;
ImageCodecInfo info = GetEncoderInfo("image/tiff");
foreach (Image img in images)
{
if (frame == 0)
{
pages = img;
//save the first frame
pages.Save(outFile, info, ep);
}
else
{
//save the intermediate frames
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);
pages.SaveAdd(img, ep);
}
if (frame == images.Length - 1)
{
//flush and close.
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);
pages.SaveAdd(ep);
}
frame++;
}
ImageCodecInfo GetEncoderInfo(string mimeType)
{
ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
for (int j = 0; j < encoders.Length; j++)
{
if (encoders[j].MimeType == mimeType)
return encoders[j];
}
throw new Exception(mimeType + " mime type not found in ImageCodecInfo");
}
}
...
JoinTiffImages(images.ToArray(), 檔名, EncoderValue.CompressionLZW);
void JoinTiffImages(Image[] images, string outFile, EncoderValue compressEncoder)
{
//use the save encoder
Encoder enc = Encoder.SaveFlag;
EncoderParameters ep = new EncoderParameters(2);
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame);
ep.Param[1] = new EncoderParameter(Encoder.Compression, (long)compressEncoder);
Image pages = images[0];
int frame = 0;
ImageCodecInfo info = GetEncoderInfo("image/tiff");
foreach (Image img in images)
{
if (frame == 0)
{
pages = img;
//save the first frame
pages.Save(outFile, info, ep);
}
else
{
//save the intermediate frames
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);
pages.SaveAdd(img, ep);
}
if (frame == images.Length - 1)
{
//flush and close.
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);
pages.SaveAdd(ep);
}
frame++;
}
ImageCodecInfo GetEncoderInfo(string mimeType)
{
ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
for (int j = 0; j < encoders.Length; j++)
{
if (encoders[j].MimeType == mimeType)
return encoders[j];
}
throw new Exception(mimeType + " mime type not found in ImageCodecInfo");
}
}
把程式加到開始畫面
產生捷徑並放到
C:\Users\登入帳號\AppData\Roaming\Microsoft\Windows\Start Menu\Programs
再從開始選單找到此捷徑並按右鍵 > 釘選到開始
C:\Users\登入帳號\AppData\Roaming\Microsoft\Windows\Start Menu\Programs
再從開始選單找到此捷徑並按右鍵 > 釘選到開始
訂閱:
文章 (Atom)
input 連結 datalist 用程式控制彈出選項
範例: nextTick(() => document.querySelector('input').showPicker()); ※僅支援現代瀏覽器
-
1. 設定檔案下載儲存位置為 C:\Users\%username%\AppData\Local\Google\Chrome\User Data\Default\Cache 2. 勾選"下載每個檔案前詢問儲存位置" 3. 針對不要下載的檔案類型於第一...
-
自動設定欄寬 sheet.Cells.AutoFitColumns(3, 20); // 必須設定 min 跟 max 才會正常作用 凍結欄位 sheet.View.FreezePanes(4, 4); 標題列 ws.PrinterSettings.RepeatRo...
-
使用 FreeSpire.XLS ... ep.Save(); using (var workbook = new Workbook()) using (var memStream = new MemoryStream()) { workbook.LoadFromSt...