如何从文件流中插入Jpg图像 [英] How Do I Insert Jpg Image From File Stream

查看:133
本文介绍了如何从文件流中插入Jpg图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var sd = new ExcelLibrary.SpreadSheet.Picture();

sd.Image = ExcelLibrary.SpreadSheet.Image.FromFile("c:\\images\\a.jpg");
ws.AddPicture(sd);





无效,它提供不受支持的格式错误。



我如何从文件和字节[] ??



is not working, it gives unsupported format error.

how do i do it from file and byte[]??

推荐答案

做代码

使用ExcelLite导出到Excel的代码。

Do code
Code to Export to Excel using ExcelLite.
WriteableBitmap bitmap = new WriteableBitmap(640,300);
 
//pass the chart control to convert it into Bitmap
 bitmap.Render(chart1, null);
 bitmap.Invalidate();
 
// open file dialog for selecting export file
 SaveFileDialog sDialog = new SaveFileDialog();
 sDialog.Filter = "Excel Files(*.xls)|*.xls";
 
if (sDialog.ShowDialog() == true)
 {
 // create a workbook object
 Workbook workbook = new Workbook();
 //Create a worksheet object
 Worksheet worksheet1 = new Worksheet("SheetWithImage");
 
// creat a spreadsheet picture object
 Lite.ExcelLibrary.SpreadSheet.Picture pic = new Lite.ExcelLibrary.SpreadSheet.Picture();
 //set its image property from silverlight image control
 // image formates 0xF01E=png,0xF01D=jpeg
 //ImageTranslator.TranslateImageToBytes translate an image control to byte array
 // that will be used by excel picture object to plot picture in excel file.
 System.Windows.Controls.Image image = new System.Windows.Controls.Image();
 image.Source = bitmap;
 image.Name = "imgExport";
 image.Width = 640;
 image.Height = 300;
 image.Stretch = Stretch.Fill;
 pic.Image = new Lite.ExcelLibrary.SpreadSheet.Image(ImageTranslator.TranslateImageToBytes(image), 0xF01E);
 
 //set picture size
 pic.TopLeftCorner = new CellAnchor(1, 1, 10, 10);
 pic.BottomRightCorner = new CellAnchor(8, 5, 10, 10);
 // add picture to spreadsheet
 worksheet1.AddPicture(pic);
 /// add worksheet to workbook
 workbook.Worksheets.Add(worksheet1);
 // get the stream of selected file
 Stream sFile = sDialog.OpenFile();
 // save excel file
 workbook.Save(sFile);
 }


这篇关于如何从文件流中插入Jpg图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆