Skip to content

Commit a26f79f

Browse files
committed
saving image in rgf support
1 parent ecac221 commit a26f79f

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

ReoGrid/Core/Cell.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,20 @@ internal void SetSingleCellData(Cell cell, object data)
235235
}
236236
}
237237

238+
// experimental: directly set an image as cell data
239+
//
240+
//else if (data is System.Drawing.Image)
241+
//{
242+
// if (cell.body == null)
243+
// {
244+
// cell.Body = new ImageCell((System.Drawing.Image)data);
245+
// }
246+
// else if (cell.body is ImageCell)
247+
// {
248+
// ((ImageCell)cell.body).Image = (System.Drawing.Image)data;
249+
// }
250+
//}
251+
238252
#if FORMULA
239253
if (formulaRanges.Count > 0)
240254
{

ReoGrid/IO/RGFFormat.cs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -651,10 +651,34 @@ public void LoadRGF(Stream s)
651651
throw new ReoGridLoadException("Cannot create cell body instance from type: " + xmlCell.bodyType, ex);
652652
}
653653
}
654+
655+
if (type == typeof(CellTypes.ImageCell))
656+
{
657+
int leftIndex = xmlCell.data.IndexOf(',');
658+
if (leftIndex > 0)
659+
{
660+
string mimetype = xmlCell.data.Substring(0, leftIndex);
661+
if (mimetype == "image/png")
662+
{
663+
string imgcode = xmlCell.data.Substring(leftIndex + 1);
664+
665+
using (var ms = new MemoryStream(Convert.FromBase64String(imgcode)))
666+
{
667+
var img = System.Drawing.Image.FromStream(ms);
668+
((CellTypes.ImageCell)cell.body).Image = img;
669+
}
670+
671+
cellValue = null;
672+
}
673+
}
674+
}
654675
}
655676
#endregion // Body
656677

657-
SetSingleCellData(cell, cellValue);
678+
if (!string.IsNullOrEmpty(cellValue))
679+
{
680+
SetSingleCellData(cell, cellValue);
681+
}
658682

659683
#if FORMULA
660684
if (!string.IsNullOrEmpty(formula))
@@ -1231,7 +1255,7 @@ from nr in this.registeredNamedRanges.Values
12311255
{
12321256
bool addCell = false;
12331257

1234-
if (cell.InnerData != null || cell.Rowspan > 1 || cell.Colspan > 1) addCell = true;
1258+
if (cell.InnerData != null || cell.Rowspan > 1 || cell.Colspan > 1 || cell.body != null) addCell = true;
12351259

12361260
RGXmlCellStyle xmlStyle = null;
12371261

@@ -1316,6 +1340,16 @@ from nr in this.registeredNamedRanges.Values
13161340
xmlCell.formula = new RGXmlCellFormual { val = (bool)cell.InnerData ? "True" : "False" };
13171341
}
13181342

1343+
if (cell.body is CellTypes.ImageCell)
1344+
{
1345+
var imageBody = (CellTypes.ImageCell)cell.body;
1346+
using (var ms = new MemoryStream())
1347+
{
1348+
imageBody.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
1349+
xmlCell.data = "image/png," + Convert.ToBase64String(ms.ToArray());
1350+
}
1351+
}
1352+
13191353
body.cells.Add(xmlCell);
13201354
}
13211355
#endregion // Cell

0 commit comments

Comments
 (0)