Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions UoFiddler.Controls/Classes/AnimatedFrameListExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public static (Point, Size) GetAnimationDetails(this IEnumerable<AnimatedFrame>
var drawCenter = new Point(int.MinValue, int.MinValue);
foreach (var frame in frames)
{
if (frame?.Bitmap == null)
{
continue;
}

drawCenter.X = Math.Max(drawCenter.X, frame.Center.X);
drawCenter.Y = Math.Max(drawCenter.Y, frame.Center.Y + frame.Bitmap.Height);
}
Expand All @@ -34,14 +39,27 @@ public static (Point, Size) GetAnimationDetails(this IEnumerable<AnimatedFrame>
var outputSize = new Size(0, 0);
foreach (var frame in frames)
{
var location = new Point(drawCenter.X - frame.Center.X, drawCenter.Y - frame.Center.Y - frame.Bitmap.Height);
if (frame?.Bitmap == null)
{
continue;
}

var location = frame.GetAlignedLocation(drawCenter);
outputSize.Width = Math.Max(outputSize.Width, location.X + frame.Bitmap.Width);
outputSize.Height = Math.Max(outputSize.Height, location.Y + frame.Bitmap.Height);
}

return (drawCenter, outputSize);
}

/// <summary>
/// Where to draw <paramref name="frame"/> so its center lands on <paramref name="drawCenter"/>.
/// </summary>
public static Point GetAlignedLocation(this AnimatedFrame frame, Point drawCenter)
{
return new Point(drawCenter.X - frame.Center.X, drawCenter.Y - frame.Center.Y - frame.Bitmap.Height);
}

public static void ToGif(this IEnumerable<AnimatedFrame> frames, string outputFile, bool looping = true, int delay = 150, bool showFrameBounds = false)
{
var (drawCenter, outputSize) = frames.GetAnimationDetails();
Expand All @@ -57,7 +75,7 @@ public static void ToGif(this IEnumerable<AnimatedFrame> frames, string outputFi

using var target = new Bitmap(outputSize.Width, outputSize.Height);
using var g = Graphics.FromImage(target);
var location = new Point(drawCenter.X - frame.Center.X, drawCenter.Y - frame.Center.Y - frame.Bitmap.Height);
var location = frame.GetAlignedLocation(drawCenter);

g.DrawImage(frame.Bitmap, location);

Expand Down
19 changes: 18 additions & 1 deletion UoFiddler.Controls/UserControls/AnimationListControl.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 31 additions & 7 deletions UoFiddler.Controls/UserControls/AnimationListControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1174,23 +1174,47 @@ private void ExportAnimationFrames(ImageFormat imageFormat)
string fileExtension = Utils.GetFileExtensionFor(imageFormat);
string fileName = Path.Combine(Options.OutputPath, $"{what} {Utils.FormatExportId(_currentSelect)}");

for (int i = 0; i < MainPictureBox.Frames?.Count; ++i)
var frames = MainPictureBox.Frames;
if (frames == null || frames.Count == 0)
{
var frameBitmap = MainPictureBox.Frames[i].Bitmap;
using (Bitmap newBitmap = new Bitmap(frameBitmap.Width, frameBitmap.Height))
return;
}

// Aligned: every frame on one canvas, anchored at its center like the Gif export
bool align = alignFramesToolStripMenuItem.Checked;
var (drawCenter, canvasSize) = align ? frames.GetAnimationDetails() : (Point.Empty, Size.Empty);
if (align && (canvasSize.Width <= 0 || canvasSize.Height <= 0))
{
return;
}

// Bmp and Jpg have no alpha
bool transparent = align && (imageFormat.Equals(ImageFormat.Png) || imageFormat.Equals(ImageFormat.Tiff));

for (int i = 0; i < frames.Count; ++i)
{
var frame = frames[i];
if (frame?.Bitmap == null)
{
continue;
}

Size size = align ? canvasSize : frame.Bitmap.Size;
Point location = align ? frame.GetAlignedLocation(drawCenter) : Point.Empty;
using (Bitmap newBitmap = new Bitmap(size.Width, size.Height))
{
using (Graphics newGraph = Graphics.FromImage(newBitmap))
{
newGraph.FillRectangle(Brushes.White, 0, 0, newBitmap.Width, newBitmap.Height);
newGraph.DrawImage(frameBitmap, new Point(0, 0));
newGraph.Save();
newGraph.Clear(transparent ? Color.Transparent : Color.White);
newGraph.DrawImage(frame.Bitmap, location);
}

newBitmap.Save($"{fileName}-{i}.{fileExtension}", imageFormat);
}
}

FileSavedDialog.Show(FindForm(), Options.OutputPath, $"Files with following format {fileName}-X.{fileExtension} saved successfully.");
string aligned = align ? $", aligned on a {canvasSize.Width}x{canvasSize.Height} canvas," : string.Empty;
FileSavedDialog.Show(FindForm(), Options.OutputPath, $"Files with following format {fileName}-X.{fileExtension}{aligned} saved successfully.");
}

private void OnClickExportFrameBmp(object sender, EventArgs e)
Expand Down