private void FillListBox() { lblFolder.Text = "Files on folder: " + Path.GetDirectoryName(openFileDialog1.FileName); DirectoryInfo di = new DirectoryInfo(Path.GetDirectoryName(openFileDialog1.FileName)); FileInfo[] Fi = di.GetFiles("*.xls"); FilesListBox.Items.Clear(); TImageInfo[] Files = new TImageInfo[Fi.Length]; for (int k = 0; k < Fi.Length; k++) { FileInfo f = Fi[k]; bool HasCrop = false; bool HasARGB = false; XlsFile x1 = new XlsFile(); bool HasImages = false; try { x1.Open(f.FullName); for (int sheet = 1; sheet <= x1.SheetCount; sheet++) { x1.ActiveSheet = sheet; for (int i = x1.ImageCount; i > 0; i--) { HasImages = true; TImageProperties ip = x1.GetImageProperties(i); if (!HasCrop) { HasCrop = GetHasCrop(ip); } TXlsImgType imgType = TXlsImgType.Unknown; using (MemoryStream ms = new MemoryStream()) { x1.GetImage(i, ref imgType, ms); FlexCel.Pdf.TPngInformation PngInfo = FlexCel.Pdf.TPdfPng.GetPngInfo(ms); if (PngInfo != null) { HasARGB = PngInfo.ColorType == 6; } } } } } catch (Exception) { Files[k] = new TImageInfo(f, false, false, false, false); continue; } Files[k] = new TImageInfo(f, true, HasCrop, HasImages, HasARGB); } FilesListBox.Items.AddRange(Files); }
private static void LoadDataBmp(byte[] Data, TXlsImgType DataType, Stream BlipData) //A BMP is a DIB with a 14 byte header. We need to split the header. { int Ofs = 0; if (BitOps.GetWord(Data, 0) == 0x4D42) { Ofs = 14; //bitmap type ("BM"). If this is not this way, we will understand this is a DIB. } BlipData.WriteByte(0xFF); //Tag BlipData.Write(Data, Ofs, Data.Length - Ofs); }
/* * TWMFBlipHeader = packed record * m_rgbUid: TMd5Digest; { The secondary, or data, UID - should always be set. } * * Metafile Blip overhead = 34 bytes. m_cb gives the number of * bytes required to store an uncompressed version of the file, m_cbSave * is the compressed size. m_mfBounds gives the boundary of all the * drawing calls within the metafile (this may just be the bounding box * or it may allow some whitespace, for a WMF this comes from the * SetWindowOrg and SetWindowExt records of the metafile). * * m_cb: integer; // Cache of the metafile size * m_rcBounds: Array[0..3] of integer; // Boundary of metafile drawing commands * m_ptSize: Array[0..1] of integer; // Size of metafile in EMUs * m_cbSave: integer; // Cache of saved size (size of m_pvBits) * m_fCompression: byte; // MSOBLIPCOMPRESSION * m_fFilter: byte; // always msofilterNone * end; */ //1 point = 12700 emu. private static void LoadDataWMF(byte[] Data, TXlsImgType DataType, Stream BlipData) { byte[] cb = BitConverter.GetBytes((UInt32)Data.Length); BlipData.Write(cb, 0, cb.Length); //This one is used only on metafiles. if (DataType == TXlsImgType.Wmf) { //On Xls format coords are 32 bits, on wmf file format they are 16. That's why we have to convert. BlipData.Write(BitConverter.GetBytes((Int32)BitConverter.ToInt16(Data, 6)), 0, 4); BlipData.Write(BitConverter.GetBytes((Int32)BitConverter.ToInt16(Data, 8)), 0, 4); BlipData.Write(BitConverter.GetBytes((Int32)BitConverter.ToInt16(Data, 10)), 0, 4); BlipData.Write(BitConverter.GetBytes((Int32)BitConverter.ToInt16(Data, 12)), 0, 4); //byte[] ptSize = {0x18,0xF0,0x01,0x00, 0x18,0xF0,0x01,0x00}; //100 points x 100 points. This one is usd on EMF byte[] ptSize = { 0x18, 0xF0, 0xFF, 0x00, 0x18, 0xF0, 0xFF, 0x00 }; //something bigger. This will be the default size. This one is usd on EMF BlipData.Write(ptSize, 0, ptSize.Length); } else { byte[] rcBounds = new byte[4 * 4]; Array.Copy(Data, 8, rcBounds, 0, rcBounds.Length); BlipData.Write(rcBounds, 0, rcBounds.Length); //ptSize int WidthMM = BitConverter.ToInt32(Data, 24 + 8) - BitConverter.ToInt32(Data, 24); int HeightMM = BitConverter.ToInt32(Data, 24 + 12) - BitConverter.ToInt32(Data, 24 + 4); byte[] WidthEmu = BitConverter.GetBytes(WidthMM * 360); BlipData.Write(WidthEmu, 0, WidthEmu.Length); byte[] HeightEmu = BitConverter.GetBytes(HeightMM * 360); BlipData.Write(HeightEmu, 0, HeightEmu.Length); } byte[] OtherDat = { 0, 0, 0, 0, 0, 254 }; long StreamPos = BlipData.Position; BlipData.Write(OtherDat, 0, 6); if (DataType == TXlsImgType.Emf) { XlsMetafiles.ToXls(Data, BlipData, true); } else { XlsMetafiles.ToXls(Data, BlipData, false); } //GoBack and set m_cbSave BlipData.Position = StreamPos; BlipData.Write(BitConverter.GetBytes((UInt32)(BlipData.Length - StreamPos - 6)), 0, 4); BlipData.Position = BlipData.Length; }
internal static void CheckImgValid(ref byte[] data, ref TXlsImgType imgType, bool AllowBmp) { TXlsImgType bmp = AllowBmp ? TXlsImgType.Unknown : TXlsImgType.Bmp; if (imgType == TXlsImgType.Unknown || imgType == bmp || imgType == TXlsImgType.Gif || imgType == TXlsImgType.Tiff) //We will try to convert bmps to png. We can't on CF { data = TCompactFramework.ImgConvert(data, ref imgType); } if (imgType == TXlsImgType.Unknown || imgType == TXlsImgType.Gif || imgType == TXlsImgType.Tiff) { FlxMessages.ThrowException(FlxErr.ErrInvalidImage); } }
internal static TEscherBSERecord Convert(byte[] Data, TXlsImgType DataType, TEscherDwgGroupCache DwgGroupCache, TEscherDwgCache DwgCache) { byte[] BSEHeader = TCompactFramework.GetBSEHeader(Data, (int)TBSEHeader.length, (int)TBSEHeader.rgbUid); using (MemoryStream BlipData = new MemoryStream()) { //Common header BlipData.Write(BSEHeader, (int)TBSEHeader.rgbUid, (int)TBSEHeader.tag - (int)TBSEHeader.rgbUid); // Specific info if ((DataType == TXlsImgType.Jpeg) || (DataType == TXlsImgType.Png)) { LoadDataBitmap(Data, DataType, BlipData); } else if (DataType == TXlsImgType.Bmp) { LoadDataBmp(Data, DataType, BlipData); } else { LoadDataWMF(Data, DataType, BlipData); } BSEHeader[(int)TBSEHeader.btWin32] = (byte)XlsEscherConsts.XlsImgConv(DataType); BSEHeader[(int)TBSEHeader.btMacOS] = (byte)msoblip.PICT; BitOps.SetWord(BSEHeader, (int)TBSEHeader.tag, 0xFF); BitOps.SetCardinal(BSEHeader, (int)TBSEHeader.size, BlipData.Length + XlsEscherConsts.SizeOfTEscherRecordHeader); BitOps.SetCardinal(BSEHeader, (int)TBSEHeader.cRef, 0); BitOps.SetCardinal(BSEHeader, (int)TBSEHeader.foDelay, 0); TEscherRecordHeader Eh = new TEscherRecordHeader(); Eh.Id = (int)Msofbt.BSE; Eh.Pre = 2 + ((int)XlsEscherConsts.XlsImgConv(DataType) << 4); Eh.Size = BitOps.GetCardinal(BSEHeader, (int)TBSEHeader.size) + BSEHeader.Length; TEscherBSERecord Result = new TEscherBSERecord(Eh, DwgGroupCache, DwgCache, DwgGroupCache.BStore); TEscherRecordHeader BlipHeader = new TEscherRecordHeader(); BlipHeader.Id = (int)XlsEscherConsts.XlsBlipHeaderConv(DataType); BlipHeader.Pre = (int)XlsEscherConsts.XlsBlipSignConv(DataType) << 4; BlipHeader.Size = BlipData.Length; BlipData.Position = 0; Result.CopyFromData(BSEHeader, BlipHeader, BlipData); return(Result); } }
private static byte[] DoImgConvert(byte[] data, ref TXlsImgType imgType) { using (MemoryStream DataStream = new MemoryStream(data)) { using (MemoryStream Result = new MemoryStream()) { using (Image Img = ImageExtender.FromStream(DataStream)) { Img.Save(Result, ImageFormat.Png); imgType = TXlsImgType.Png; return(Result.ToArray()); } } } }
public static byte[] ImgConvert(byte[] data, ref TXlsImgType imgType) { if (MissingFrameworkImageSave) { return(data); } try { byte[] Result = DoImgConvert(data, ref imgType); return(Result); } catch (MissingMethodException) { MissingFrameworkImageSave = true; //Nothing. It could be a bmp, and it is valid. (even when it would be better to convert it to a png) } catch (ArgumentException) { FlxMessages.ThrowException(FlxErr.ErrInvalidImage); } return(data); }
private void OpenFile(string FileName) { ImageDataTable.Rows.Clear(); try { XlsFile Xls = new XlsFile(true); CurrentFilename = FileName; Xls.Open(FileName); for (int sheet = 1; sheet <= Xls.SheetCount; sheet++) { Xls.ActiveSheet = sheet; for (int i = Xls.ImageCount; i > 0; i--) { TXlsImgType ImageType = TXlsImgType.Unknown; byte[] ImgBytes = Xls.GetImage(i, ref ImageType); TImageProperties ImgProps = Xls.GetImageProperties(i); object[] ImgData = new object[ImageDataTable.Columns.Count]; ImgData[0] = Xls.SheetName; ImgData[1] = i; ImgData[4] = ImageType.ToString(); ImgData[7] = Xls.GetImageName(i); ImgData[8] = ImgBytes; ImgData[9] = GetHasCrop(ImgProps); using (MemoryStream ms = new MemoryStream(ImgBytes)) { FlexCel.Pdf.TPngInformation PngInfo = FlexCel.Pdf.TPdfPng.GetPngInfo(ms); if (PngInfo != null) { ImgData[2] = PngInfo.Width; ImgData[3] = PngInfo.Height; string s = String.Empty; int bpp = 0; if ((PngInfo.ColorType & 4) != 0) { s += "ALPHA-"; bpp = 1; } if ((PngInfo.ColorType & 2) == 0) { s += "Grayscale -" + (1 << PngInfo.BitDepth).ToString() + " shades. "; bpp = 1; } else { if ((PngInfo.ColorType & 1) == 0) { bpp += 3; s += "RGB - " + (PngInfo.BitDepth * (bpp)).ToString() + "bpp. "; } else { s += "Indexed - " + (1 << PngInfo.BitDepth).ToString() + " colors. "; bpp = 1; } } ImgData[5] = s; ImgData[6] = (Math.Round(PngInfo.Width * PngInfo.Height * PngInfo.BitDepth * bpp / 8f / 1024f)).ToString() + " kb."; } else { ms.Position = 0; try { using (Image Img = Image.FromStream(ms)) { Bitmap Bmp = Img as Bitmap; if (Bmp != null) { ImgData[5] = Bmp.PixelFormat.ToString() + "bpp"; } ImgData[2] = Img.Width; ImgData[3] = Img.Height; } } catch (Exception) { ImgData[2] = -1; ImgData[3] = -1; ImgData[5] = null; ImgData[8] = null; } } } ImageDataTable.Rows.Add(ImgData); } } } catch (Exception ex) { MessageBox.Show(ex.Message, "Error"); dataGrid.CaptionText = "No file selected"; CurrentFilename = null; return; } dataGrid.CaptionText = "Selected file: " + FileName; CurrentRowChanged(GetCurrencyManager, null); }
public static byte[] ImgConvert(byte[] data, ref TXlsImgType imgType) { return(data); }
private static void LoadDataBitmap(byte[] Data, TXlsImgType DataType, Stream BlipData) { BlipData.WriteByte(0xFF); //Tag BlipData.Write(Data, 0, Data.Length); }