Skip to content

Commit

Permalink
fix: name frmMain
Browse files Browse the repository at this point in the history
  • Loading branch information
heliomarpm committed Oct 15, 2023
1 parent 45a595f commit 9599d8e
Show file tree
Hide file tree
Showing 8 changed files with 211 additions and 199 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ jobs:

# Autobuild attempts to build any compiled languages (C/C++, C#, Go, Java, or Swift).
# If this step fails, then you should remove it and run the build manually (see below)
# - name: Autobuild
# uses: github/codeql-action/autobuild@v2
- name: Autobuild
uses: github/codeql-action/autobuild@v2

# ℹ️ Command-line programs to run using the OS shell.
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun
Expand Down
2 changes: 0 additions & 2 deletions ResizeImages.Core/GenPackageFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ private void SavePdf(string inputPath, string pathSaveFile)
wPdf.DirectContent.AddImage(img);
//}
}

}

//save file
Expand All @@ -108,7 +107,6 @@ private void SavePdf(string inputPath, string pathSaveFile)

wPdf.Close();
wPdf.Dispose();

}

/*
Expand Down
276 changes: 138 additions & 138 deletions ResizeImages.Core/GenerateImages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ private void SaveBackup(string pathFile, string backFolder)

private ResizeScale ResetScale(ResizeScale scale, int widthImgOriginal, int heightImgOriginal)
{
//' create New image and bitmap objects. Load the image file and put into a resized bitmap.
bool retrato = widthImgOriginal <= heightImgOriginal;

if (scale.Width < 1) scale.Width = widthImgOriginal;
Expand All @@ -70,160 +69,161 @@ private ResizeScale ResetScale(ResizeScale scale, int widthImgOriginal, int heig
float width = scale.Width;
float height = scale.Height;

// Se é pra manter a orientação então inverte se estiver diferente da imagem original
// Se é pra manter a orientação, inverte a largura e a altura se a orientação deve ser preservada
if (scale.PreserveOrientation && (width <= height && !retrato))
{
width = scale.Height;
height = scale.Width;
{
(width, height) = (height, width);
}

if (scale.IsPercentage)
{
width = widthImgOriginal * (width / 100);
height = heightImgOriginal * (height / 100);
}
else
{
// Se deve manter proporção para evitar distorção na imagem
if (scale.PreserveAspectRatio)
{
float percetual;

if (width > 0)
{
if (width == widthImgOriginal)
{
height = heightImgOriginal; //mantem original
}
else
{
percetual = (float)width / widthImgOriginal;
height = (heightImgOriginal * percetual);
}
}
else if (height > 0)
{
if (height == heightImgOriginal)
{
width = widthImgOriginal; //mantem original
}
else
{
percetual = (float)height / heightImgOriginal;
width = (widthImgOriginal * percetual);
}
}
}
}

if (width == 0 || height == 0)
{
width = widthImgOriginal;
height = heightImgOriginal;
}

else if (scale.PreserveAspectRatio)
{
if (width > 0)
{
//if (width == widthImgOriginal)
//{
// height = heightImgOriginal; //mantem original
//}
//else
//{
//float percetual = (float)width / widthImgOriginal;
height = width == widthImgOriginal
? heightImgOriginal
: heightImgOriginal * (width / widthImgOriginal);
//}
}
else if (height > 0)
{
//if (height == heightImgOriginal)
//{
// width = widthImgOriginal; //mantem original
//}
//else
//{
//float percetual = (float)height / heightImgOriginal;
width = height == heightImgOriginal
? widthImgOriginal
: widthImgOriginal * (height / heightImgOriginal);
//}
}
}


//if (width == 0 || height == 0)
//{
// width = widthImgOriginal;
// height = heightImgOriginal;
//}
// Garante que width e height sejam valores válidos
if (width <= 0) width = widthImgOriginal;
if (height <= 0) height = heightImgOriginal;

scale.Width = width;
scale.Height = height;
return scale;
}
private static ImageCodecInfo GetEncoderInfo(ImageFormat imageFormat)
{
string mimeType = $"image/{imageFormat.ToString().ToLower()}";

// Get image codecs for all image formats
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();

// Find the correct image codec
for (int i = 0; i < codecs.Length; i++)
if (codecs[i].MimeType.ToLower() == mimeType)
return codecs[i];

return null;
}

public string Save(string inputFile, ResizeScale scale, string outputFile = null, string pathBackup = null)
{
if (string.IsNullOrEmpty(outputFile))
outputFile = inputFile;

string inPath = Path.GetDirectoryName(inputFile);
string outPath = Path.GetDirectoryName(outputFile);
string outFile = Path.GetFileName(outputFile);

if (string.IsNullOrEmpty(outFile))
outFile = Path.GetFileName(inputFile);

outputFile = Path.Combine(outPath, outFile);

}

private static ImageCodecInfo GetEncoderInfo(ImageFormat imageFormat)
{
string mimeType = $"image/{imageFormat.ToString().ToLower()}";

// Get image codecs for all image formats
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();

// Find the correct image codec
for (int i = 0; i < codecs.Length; i++)
if (codecs[i].MimeType.ToLower() == mimeType)
return codecs[i];

return null;
}

public string Save(string inputFile, ResizeScale scale, string outputFile = null, string pathBackup = null)
{
if (string.IsNullOrEmpty(outputFile))
outputFile = inputFile;

string inPath = Path.GetDirectoryName(inputFile);
string outPath = Path.GetDirectoryName(outputFile);
string outFile = Path.GetFileName(outputFile);

if (string.IsNullOrEmpty(outFile))
outFile = Path.GetFileName(inputFile);

outputFile = Path.Combine(outPath, outFile);

#region "SE SOLICITADO, SALVA BACKUP ANTES DE ALTERAR."

if (!string.IsNullOrEmpty(pathBackup))
{
pathBackup = Path.GetDirectoryName(pathBackup); // normaliza o valor

if (inPath != pathBackup)
{
if (outputFile == Path.Combine(pathBackup, Path.GetFileName(inputFile)))
throw new ArgumentException("Local de Backup não pode ser o mesmo do local de destino");

this.SaveBackup(inputFile, pathBackup);
}
}


if (!string.IsNullOrEmpty(pathBackup))
{
pathBackup = Path.GetDirectoryName(pathBackup); // normaliza o valor

if (inPath != pathBackup)
{
if (outputFile == Path.Combine(pathBackup, Path.GetFileName(inputFile)))
throw new ArgumentException("Local de Backup não pode ser o mesmo do local de destino");

this.SaveBackup(inputFile, pathBackup);
}
}

#endregion


#region "CRIA IMAGEM COM NOVAS DIMENSOES"

// cria o diretorio de output se necessario
if (!Directory.Exists(outPath))
Directory.CreateDirectory(outPath);

//outPath = $@"{outPath}\{Path.GetFileName(inputFile)}";

Bitmap imgOutput;
ImageCodecInfo imgCodec;

using (var img = Image.FromFile(inputFile))
{
imgCodec = GetEncoderInfo(img.RawFormat);
scale = ResetScale(scale, img.Width, img.Height);
imgOutput = new Bitmap(img, (int)scale.Width, (int)scale.Height);
}

// Para evitar o erro de GDI+ quando o caminho é muito longo
using (MemoryStream ms = new MemoryStream())
{
imgOutput.Save(ms, imgCodec, null);
imgOutput.Dispose();

// Se arquivo estiver bloqueado cria um nome aleatorio
//if (!Utils.DeleteFile(outputFile))
//{
// outputFile = $@"{Path.GetFileNameWithoutExtension(outputFile)}_{Guid.NewGuid().ToString().Substring(0, 8)}{Path.GetExtension(outputFile)}";
// outputFile = Path.Combine(outPath, outputFile);
//}

File.WriteAllBytes(outputFile, ms.ToArray());
}


// cria o diretorio de output se necessario
if (!Directory.Exists(outPath))
Directory.CreateDirectory(outPath);

//outPath = $@"{outPath}\{Path.GetFileName(inputFile)}";

Bitmap imgOutput;
ImageCodecInfo imgCodec;

using (var img = Image.FromFile(inputFile))
{
imgCodec = GetEncoderInfo(img.RawFormat);
scale = ResetScale(scale, img.Width, img.Height);
imgOutput = new Bitmap(img, (int)scale.Width, (int)scale.Height);
}

// Para evitar o erro de GDI+ quando o caminho é muito longo
using (MemoryStream ms = new MemoryStream())
{
imgOutput.Save(ms, imgCodec, null);
imgOutput.Dispose();

// Se arquivo estiver bloqueado cria um nome aleatorio
//if (!Utils.DeleteFile(outputFile))
//{
// outputFile = $@"{Path.GetFileNameWithoutExtension(outputFile)}_{Guid.NewGuid().ToString().Substring(0, 8)}{Path.GetExtension(outputFile)}";
// outputFile = Path.Combine(outPath, outputFile);
//}

File.WriteAllBytes(outputFile, ms.ToArray());
}

#endregion


#region "GUARDA LISTA DOS ARQUIVOS PROCESSADOS"

if (!this.Output.ContainsKey(inPath))
this.Output.Add(inPath, new Dictionary<string, List<string>>());

if (!this.Output[inPath].ContainsKey(outPath))
this.Output[inPath].Add(outPath, new List<string>());

this.Output[inPath][outPath].Add(outputFile);

this.Outputs.Add(outputFile);


if (!this.Output.ContainsKey(inPath))
this.Output.Add(inPath, new Dictionary<string, List<string>>());

if (!this.Output[inPath].ContainsKey(outPath))
this.Output[inPath].Add(outPath, new List<string>());

this.Output[inPath][outPath].Add(outputFile);

this.Outputs.Add(outputFile);

#endregion

return outputFile;

return outputFile;
}

}
}
4 changes: 4 additions & 0 deletions ResizeImages.Core/ResizeImages.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<None Include="..\.editorconfig" Link=".editorconfig" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.Drawing.Common" Version="5.0.3" />
</ItemGroup>
Expand Down
Loading

0 comments on commit 9599d8e

Please sign in to comment.