Skip to content

Commit

Permalink
Merge pull request #637 from FastReports/sync_branch_2023.3.7
Browse files Browse the repository at this point in the history
FastReport.OpenSource 2023.3.7
  • Loading branch information
0legK authored Oct 18, 2023
2 parents d7e2035 + 43cdc7a commit f23de48
Show file tree
Hide file tree
Showing 2 changed files with 181 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,125 @@ public override DbDataAdapter GetAdapter(string selectCommand, DbConnection conn
{
MySqlDataAdapter adapter = new MySqlDataAdapter(selectCommand, connection as MySqlConnection);
foreach (CommandParameter p in parameters)
{
{
MySqlParameter parameter = adapter.SelectCommand.Parameters.Add(p.Name, (MySqlDbType)p.DataType, p.Size);
parameter.Value = p.Value;

if (p.Value is Variant value)
{
//https://mysqlconnector.net/troubleshooting/parameter-types/
if (value.Type == typeof(string))
parameter.Value = VariantToClrType(value, (MySqlDbType)p.DataType);
else
parameter.Value = value.ToType(value.Type);
}
else
parameter.Value = p.Value;
}
return adapter;
}

private object VariantToClrType(Variant value, MySqlDbType type)
{
if (value.ToString() == "")
return null;

switch (type)
{
case MySqlDbType.Int64:
{
long val = 0;
long.TryParse(value.ToString(), out val);
return val;
}
case MySqlDbType.UInt64:
{
ulong val = 0;
ulong.TryParse(value.ToString(), out val);
return val;
}
case MySqlDbType.Bit:
case MySqlDbType.Bool:
{
bool val = false;
bool.TryParse(value.ToString(), out val);
if (value.ToString() == "1")
val = true;
return val;
}
case MySqlDbType.Double:
{
double val = 0;
double.TryParse(value.ToString(), out val);
return val;
}
case MySqlDbType.DateTime:
case MySqlDbType.Date:
{
DateTime val = DateTime.Now;
DateTime.TryParse(value.ToString(), out val);
return val;
}
case MySqlDbType.Time:
case MySqlDbType.Timestamp:
{
TimeSpan val = TimeSpan.Zero;
TimeSpan.TryParse(value.ToString(), out val);
return val;
}
case MySqlDbType.Decimal:
{
decimal val = 0;
decimal.TryParse(value.ToString(), out val);
return val;
}
case MySqlDbType.Float:
{
float val = 0;
float.TryParse(value.ToString(), out val);
return val;
}
case MySqlDbType.Int24:
case MySqlDbType.UInt24:
case MySqlDbType.Int32:
{
int val = 0;
int.TryParse(value.ToString(), out val);
return val;
}
case MySqlDbType.UInt32:
{
uint val = 0;
uint.TryParse(value.ToString(), out val);
return val;
}
case MySqlDbType.Guid:
{
Guid val = Guid.Empty;
Guid.TryParse(value.ToString(), out val);
return val;
}
case MySqlDbType.Int16:
{
short val = 0;
short.TryParse(value.ToString(), out val);
return val;
}
case MySqlDbType.UInt16:
{
ushort val = 0;
ushort.TryParse(value.ToString(), out val);
return val;
}
case MySqlDbType.Byte:
case MySqlDbType.UByte:
{
byte val = 0;
byte.TryParse(value.ToString(), out val);
return val;
}
default:
return value.ToString();
}
}
}
}
77 changes: 65 additions & 12 deletions FastReport.Base/Export/Image/ImageExport.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
Expand Down Expand Up @@ -72,6 +73,10 @@ public partial class ImageExport : ExportBase
private float zoomX;
private float zoomY;
private System.Drawing.Drawing2D.GraphicsState state;
private string imageExtensionFormat;
private string documentTitle;

private bool saveStreams;

const float DIVIDER = 0.75f;
const float PAGE_DIVIDER = 2.8346400000000003f; // mm to point
Expand Down Expand Up @@ -210,6 +215,16 @@ private bool IsMultiFrameTiff
{
get { return ImageFormat == ImageExportFormat.Tiff && MultiFrameTiff; }
}

/// <summary>
/// Enable or disable saving streams in GeneratedStreams collection.
/// </summary>
public bool SaveStreams
{
get { return saveStreams; }
set { saveStreams = value; }
}

#endregion

#region Private Methods
Expand All @@ -220,11 +235,24 @@ private System.Drawing.Image CreateImage(int width, int height, string suffix)
return CreateMetafile(suffix);
return new Bitmap(width, height);
}
private void GeneratedUpdate(string filename, Stream stream)
{
int i = GeneratedFiles.IndexOf(filename);
if (i == -1)
{
GeneratedFiles.Add(filename);
GeneratedStreams.Add(stream);
}
else
{
GeneratedStreams[i] = stream;
}
}

private System.Drawing.Image CreateMetafile(string suffix)
{
string extension = Path.GetExtension(FileName);
string fileName = Path.ChangeExtension(FileName, suffix + extension);
string targetFileName = Path.ChangeExtension(FileName, suffix + extension);

System.Drawing.Image image;
using (Bitmap bmp = new Bitmap(1, 1))
Expand All @@ -235,9 +263,9 @@ private System.Drawing.Image CreateMetafile(string suffix)
image = new Metafile(Stream, hdc);
else
{
image = new Metafile(fileName, hdc);
if (!GeneratedFiles.Contains(fileName))
GeneratedFiles.Add(fileName);
image = new Metafile(targetFileName, hdc);
if (!GeneratedFiles.Contains(targetFileName))
GeneratedFiles.Add(targetFileName);
}
g.ReleaseHdc(hdc);
}
Expand Down Expand Up @@ -383,17 +411,31 @@ private void SaveImage(System.Drawing.Image image, string suffix)
}
else if (ImageFormat != ImageExportFormat.Metafile)
{
string extension = Path.GetExtension(FileName);
string fileName = Path.ChangeExtension(FileName, suffix + extension);
Stream stream;
string targetFileName;
if (saveStreams)
{
targetFileName = Path.ChangeExtension(documentTitle + suffix, imageExtensionFormat);
stream = new MemoryStream();
}
else
{
string extension = Path.GetExtension(FileName);
targetFileName = Path.ChangeExtension(FileName, suffix + extension);

// empty suffix means that we should use the Stream that was created in the ExportBase
Stream stream = suffix == "" ? Stream : new FileStream(fileName, FileMode.Create);
// empty suffix means that we should use the Stream that was created in the ExportBase
stream = suffix == "" ? Stream : new FileStream(targetFileName, FileMode.Create);

if (suffix != "")
GeneratedFiles.Add(fileName);
if (suffix != "")
{
GeneratedFiles.Add(targetFileName);
}
}

if (ImageFormat == ImageExportFormat.Jpeg)
{
ExportUtils.SaveJpeg(image, stream, JpegQuality);
}
else if (ImageFormat == ImageExportFormat.Tiff && MonochromeTiff)
{
// handle monochrome tiff separately
Expand Down Expand Up @@ -426,7 +468,9 @@ private void SaveImage(System.Drawing.Image image, string suffix)
image.Save(stream, format);
}

if (suffix != "")
if (saveStreams)
GeneratedUpdate(targetFileName, stream);
else if (suffix != "")
stream.Dispose();
}

Expand All @@ -450,6 +494,7 @@ protected override void Start()

//init
SeparateFiles = Stream is MemoryStream ? false : SeparateFiles;
GeneratedStreams = new List<Stream>();
pageNumber = 0;
height = 0;
width = 0;
Expand All @@ -462,6 +507,13 @@ protected override void Start()
curOriginY = 0;
firstPage = true;

if (saveStreams)
{
imageExtensionFormat = ImageFormat.ToString();
separateFiles = true;
documentTitle = (!String.IsNullOrEmpty(Report.ReportInfo.Name) ?
Report.ReportInfo.Name : "");
}
if (!SeparateFiles && !IsMultiFrameTiff)
{
// create one big image. To do this, calculate max width and sum of pages height
Expand Down Expand Up @@ -510,7 +562,7 @@ protected override void ExportPageBegin(ReportPage page)
else
g = Graphics.FromImage(image);

state = g.Save();
state = g.Save();

g.FillRegion(Brushes.Transparent, new Region(new RectangleF(0, curOriginY, width, height)));
if (bigImage != null && curOriginY + height * 2 > bigImage.Height)
Expand Down Expand Up @@ -663,6 +715,7 @@ public ImageExport()
Resolution = 96;
jpegQuality = 100;
monochromeTiffCompression = EncoderValue.CompressionCCITT4;
saveStreams = false;
}
}
}

0 comments on commit f23de48

Please sign in to comment.