GIF image cropping in c# without add-ons












6















I'm trying to crop gif images in c# without using an add-ons with drawing a rectangle on the image (and I've got it to work with normal image), but .. lets just say the output is less than optimal..
examples:



Origianl Image:
Origianl Image



Cropped Image:
Cropped Image



and here's the code that I'm using :



public override Image Crop(Rectangle f, bool isStrch, int slashwid, int slashhei)
{
_revert = Image;
GifDecoder();
widmult = Width / (double)slashwid;
heimult = Height / (double)slashhei;
double num = (double)f.X * widmult;
double num2 = (double)f.Y * heimult;
double num3 = (double)f.Width * widmult;
double num4 = (double)f.Height * heimult;
Rectangle srcRect = new Rectangle(isStrch ? ((int)num) : f.X, isStrch ? ((int)num2) : f.Y, isStrch ? ((int)num3) : f.Width, isStrch ? ((int)num4) : f.Height);
CroppedFrames = new Image[Frames.Length];
for (int i = 0; i < Frames.Length; i++)
{

Bitmap image = new Bitmap(Frames[i]);
Bitmap bitmap = new Bitmap(srcRect.Width, srcRect.Height);
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.DrawImage(image, new Rectangle(0, 0, bitmap.Width, bitmap.Height), srcRect, GraphicsUnit.Pixel);
}
CroppedFrames[i] = bitmap;
}
GifEncoder(f.Width,f.Height);
return Image;
}


private void GifDecoder()
{
Stream bitmapStream = new FileStream(Path, FileMode.Open, FileAccess.Read, FileShare.Read);
GifBitmapDecoder gifBitmapDecoder = new GifBitmapDecoder(bitmapStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
Frames = new Image[gifBitmapDecoder.Frames.Count];
for (int i = 0; i < gifBitmapDecoder.Frames.Count; i++)
{
Frames[i] = BitmapFromSource(gifBitmapDecoder.Frames[i]);
}
}

private void GifEncoder(int width,int height)
{
BitmapPalette palette = new BitmapPalette(BitmapImageFromImage(Image),256);

int bytecount = (width * height) / 8;
using (FileStream fs = new FileStream(Path + "output.gif", FileMode.Create))
{
GifBitmapEncoder encoder = new GifBitmapEncoder();

for (int f = 0; f < Frames.Length; f++)
{
byte pixels = imageToByteArray(CroppedFrames[f]);

BitmapSource image = BitmapSource.Create(width,height,96,96,System.Windows.Media.PixelFormats.Indexed8,palette,pixels,width);

encoder.Frames.Add(BitmapFrame.Create(image));

}
encoder.Save(fs);
Image = Image.FromStream(fs);
fs.Close();

}
}


private byte imageToByteArray(Image imageIn)
{
MemoryStream ms = new MemoryStream();
Bitmap aaa = new Bitmap(imageIn);
aaa.Save(ms, ImageFormat.Png);
return ms.ToArray();
}


(bool isStrch, int slashwid, int slashhei) are for checking if the picturebox is smaller than the image or not to get accurate crops with the rect.










share|improve this question

























  • I do not speak C#,but my best bet here is that you are missing the alpha channel. Note how the original gif has transparency but your output has no information about that. By not getting RGBA, and instead reading as RGB, you are reading random data thus looks crap.

    – Ander Biguri
    Nov 21 '18 at 11:14











  • @AnderBiguri i actually managed to get it working but, it still doesn't loop

    – A Loli
    Nov 21 '18 at 17:22













  • So... you did not manage to get it working?

    – Ander Biguri
    Nov 21 '18 at 17:33











  • at this point yes, but it still doesn't loop, anyway this isn't my question anyway so this should be closed

    – A Loli
    Nov 22 '18 at 18:38








  • 1





    @AnderBiguri Yes. Gif format has a specific system where one colour index gets indicated as being "the transparent colour", and all pixels of that value are treated as transparent. In fact, there are more formats like that; png even supports alpha values for all of its palette indices, but .Net is equally dumb about that format and also treats it as ARGB.

    – Nyerguds
    Nov 28 '18 at 22:10


















6















I'm trying to crop gif images in c# without using an add-ons with drawing a rectangle on the image (and I've got it to work with normal image), but .. lets just say the output is less than optimal..
examples:



Origianl Image:
Origianl Image



Cropped Image:
Cropped Image



and here's the code that I'm using :



public override Image Crop(Rectangle f, bool isStrch, int slashwid, int slashhei)
{
_revert = Image;
GifDecoder();
widmult = Width / (double)slashwid;
heimult = Height / (double)slashhei;
double num = (double)f.X * widmult;
double num2 = (double)f.Y * heimult;
double num3 = (double)f.Width * widmult;
double num4 = (double)f.Height * heimult;
Rectangle srcRect = new Rectangle(isStrch ? ((int)num) : f.X, isStrch ? ((int)num2) : f.Y, isStrch ? ((int)num3) : f.Width, isStrch ? ((int)num4) : f.Height);
CroppedFrames = new Image[Frames.Length];
for (int i = 0; i < Frames.Length; i++)
{

Bitmap image = new Bitmap(Frames[i]);
Bitmap bitmap = new Bitmap(srcRect.Width, srcRect.Height);
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.DrawImage(image, new Rectangle(0, 0, bitmap.Width, bitmap.Height), srcRect, GraphicsUnit.Pixel);
}
CroppedFrames[i] = bitmap;
}
GifEncoder(f.Width,f.Height);
return Image;
}


private void GifDecoder()
{
Stream bitmapStream = new FileStream(Path, FileMode.Open, FileAccess.Read, FileShare.Read);
GifBitmapDecoder gifBitmapDecoder = new GifBitmapDecoder(bitmapStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
Frames = new Image[gifBitmapDecoder.Frames.Count];
for (int i = 0; i < gifBitmapDecoder.Frames.Count; i++)
{
Frames[i] = BitmapFromSource(gifBitmapDecoder.Frames[i]);
}
}

private void GifEncoder(int width,int height)
{
BitmapPalette palette = new BitmapPalette(BitmapImageFromImage(Image),256);

int bytecount = (width * height) / 8;
using (FileStream fs = new FileStream(Path + "output.gif", FileMode.Create))
{
GifBitmapEncoder encoder = new GifBitmapEncoder();

for (int f = 0; f < Frames.Length; f++)
{
byte pixels = imageToByteArray(CroppedFrames[f]);

BitmapSource image = BitmapSource.Create(width,height,96,96,System.Windows.Media.PixelFormats.Indexed8,palette,pixels,width);

encoder.Frames.Add(BitmapFrame.Create(image));

}
encoder.Save(fs);
Image = Image.FromStream(fs);
fs.Close();

}
}


private byte imageToByteArray(Image imageIn)
{
MemoryStream ms = new MemoryStream();
Bitmap aaa = new Bitmap(imageIn);
aaa.Save(ms, ImageFormat.Png);
return ms.ToArray();
}


(bool isStrch, int slashwid, int slashhei) are for checking if the picturebox is smaller than the image or not to get accurate crops with the rect.










share|improve this question

























  • I do not speak C#,but my best bet here is that you are missing the alpha channel. Note how the original gif has transparency but your output has no information about that. By not getting RGBA, and instead reading as RGB, you are reading random data thus looks crap.

    – Ander Biguri
    Nov 21 '18 at 11:14











  • @AnderBiguri i actually managed to get it working but, it still doesn't loop

    – A Loli
    Nov 21 '18 at 17:22













  • So... you did not manage to get it working?

    – Ander Biguri
    Nov 21 '18 at 17:33











  • at this point yes, but it still doesn't loop, anyway this isn't my question anyway so this should be closed

    – A Loli
    Nov 22 '18 at 18:38








  • 1





    @AnderBiguri Yes. Gif format has a specific system where one colour index gets indicated as being "the transparent colour", and all pixels of that value are treated as transparent. In fact, there are more formats like that; png even supports alpha values for all of its palette indices, but .Net is equally dumb about that format and also treats it as ARGB.

    – Nyerguds
    Nov 28 '18 at 22:10
















6












6








6








I'm trying to crop gif images in c# without using an add-ons with drawing a rectangle on the image (and I've got it to work with normal image), but .. lets just say the output is less than optimal..
examples:



Origianl Image:
Origianl Image



Cropped Image:
Cropped Image



and here's the code that I'm using :



public override Image Crop(Rectangle f, bool isStrch, int slashwid, int slashhei)
{
_revert = Image;
GifDecoder();
widmult = Width / (double)slashwid;
heimult = Height / (double)slashhei;
double num = (double)f.X * widmult;
double num2 = (double)f.Y * heimult;
double num3 = (double)f.Width * widmult;
double num4 = (double)f.Height * heimult;
Rectangle srcRect = new Rectangle(isStrch ? ((int)num) : f.X, isStrch ? ((int)num2) : f.Y, isStrch ? ((int)num3) : f.Width, isStrch ? ((int)num4) : f.Height);
CroppedFrames = new Image[Frames.Length];
for (int i = 0; i < Frames.Length; i++)
{

Bitmap image = new Bitmap(Frames[i]);
Bitmap bitmap = new Bitmap(srcRect.Width, srcRect.Height);
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.DrawImage(image, new Rectangle(0, 0, bitmap.Width, bitmap.Height), srcRect, GraphicsUnit.Pixel);
}
CroppedFrames[i] = bitmap;
}
GifEncoder(f.Width,f.Height);
return Image;
}


private void GifDecoder()
{
Stream bitmapStream = new FileStream(Path, FileMode.Open, FileAccess.Read, FileShare.Read);
GifBitmapDecoder gifBitmapDecoder = new GifBitmapDecoder(bitmapStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
Frames = new Image[gifBitmapDecoder.Frames.Count];
for (int i = 0; i < gifBitmapDecoder.Frames.Count; i++)
{
Frames[i] = BitmapFromSource(gifBitmapDecoder.Frames[i]);
}
}

private void GifEncoder(int width,int height)
{
BitmapPalette palette = new BitmapPalette(BitmapImageFromImage(Image),256);

int bytecount = (width * height) / 8;
using (FileStream fs = new FileStream(Path + "output.gif", FileMode.Create))
{
GifBitmapEncoder encoder = new GifBitmapEncoder();

for (int f = 0; f < Frames.Length; f++)
{
byte pixels = imageToByteArray(CroppedFrames[f]);

BitmapSource image = BitmapSource.Create(width,height,96,96,System.Windows.Media.PixelFormats.Indexed8,palette,pixels,width);

encoder.Frames.Add(BitmapFrame.Create(image));

}
encoder.Save(fs);
Image = Image.FromStream(fs);
fs.Close();

}
}


private byte imageToByteArray(Image imageIn)
{
MemoryStream ms = new MemoryStream();
Bitmap aaa = new Bitmap(imageIn);
aaa.Save(ms, ImageFormat.Png);
return ms.ToArray();
}


(bool isStrch, int slashwid, int slashhei) are for checking if the picturebox is smaller than the image or not to get accurate crops with the rect.










share|improve this question
















I'm trying to crop gif images in c# without using an add-ons with drawing a rectangle on the image (and I've got it to work with normal image), but .. lets just say the output is less than optimal..
examples:



Origianl Image:
Origianl Image



Cropped Image:
Cropped Image



and here's the code that I'm using :



public override Image Crop(Rectangle f, bool isStrch, int slashwid, int slashhei)
{
_revert = Image;
GifDecoder();
widmult = Width / (double)slashwid;
heimult = Height / (double)slashhei;
double num = (double)f.X * widmult;
double num2 = (double)f.Y * heimult;
double num3 = (double)f.Width * widmult;
double num4 = (double)f.Height * heimult;
Rectangle srcRect = new Rectangle(isStrch ? ((int)num) : f.X, isStrch ? ((int)num2) : f.Y, isStrch ? ((int)num3) : f.Width, isStrch ? ((int)num4) : f.Height);
CroppedFrames = new Image[Frames.Length];
for (int i = 0; i < Frames.Length; i++)
{

Bitmap image = new Bitmap(Frames[i]);
Bitmap bitmap = new Bitmap(srcRect.Width, srcRect.Height);
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.DrawImage(image, new Rectangle(0, 0, bitmap.Width, bitmap.Height), srcRect, GraphicsUnit.Pixel);
}
CroppedFrames[i] = bitmap;
}
GifEncoder(f.Width,f.Height);
return Image;
}


private void GifDecoder()
{
Stream bitmapStream = new FileStream(Path, FileMode.Open, FileAccess.Read, FileShare.Read);
GifBitmapDecoder gifBitmapDecoder = new GifBitmapDecoder(bitmapStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
Frames = new Image[gifBitmapDecoder.Frames.Count];
for (int i = 0; i < gifBitmapDecoder.Frames.Count; i++)
{
Frames[i] = BitmapFromSource(gifBitmapDecoder.Frames[i]);
}
}

private void GifEncoder(int width,int height)
{
BitmapPalette palette = new BitmapPalette(BitmapImageFromImage(Image),256);

int bytecount = (width * height) / 8;
using (FileStream fs = new FileStream(Path + "output.gif", FileMode.Create))
{
GifBitmapEncoder encoder = new GifBitmapEncoder();

for (int f = 0; f < Frames.Length; f++)
{
byte pixels = imageToByteArray(CroppedFrames[f]);

BitmapSource image = BitmapSource.Create(width,height,96,96,System.Windows.Media.PixelFormats.Indexed8,palette,pixels,width);

encoder.Frames.Add(BitmapFrame.Create(image));

}
encoder.Save(fs);
Image = Image.FromStream(fs);
fs.Close();

}
}


private byte imageToByteArray(Image imageIn)
{
MemoryStream ms = new MemoryStream();
Bitmap aaa = new Bitmap(imageIn);
aaa.Save(ms, ImageFormat.Png);
return ms.ToArray();
}


(bool isStrch, int slashwid, int slashhei) are for checking if the picturebox is smaller than the image or not to get accurate crops with the rect.







c# image image-processing crop gif






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 18:12







A Loli

















asked Nov 20 '18 at 16:21









A LoliA Loli

313




313













  • I do not speak C#,but my best bet here is that you are missing the alpha channel. Note how the original gif has transparency but your output has no information about that. By not getting RGBA, and instead reading as RGB, you are reading random data thus looks crap.

    – Ander Biguri
    Nov 21 '18 at 11:14











  • @AnderBiguri i actually managed to get it working but, it still doesn't loop

    – A Loli
    Nov 21 '18 at 17:22













  • So... you did not manage to get it working?

    – Ander Biguri
    Nov 21 '18 at 17:33











  • at this point yes, but it still doesn't loop, anyway this isn't my question anyway so this should be closed

    – A Loli
    Nov 22 '18 at 18:38








  • 1





    @AnderBiguri Yes. Gif format has a specific system where one colour index gets indicated as being "the transparent colour", and all pixels of that value are treated as transparent. In fact, there are more formats like that; png even supports alpha values for all of its palette indices, but .Net is equally dumb about that format and also treats it as ARGB.

    – Nyerguds
    Nov 28 '18 at 22:10





















  • I do not speak C#,but my best bet here is that you are missing the alpha channel. Note how the original gif has transparency but your output has no information about that. By not getting RGBA, and instead reading as RGB, you are reading random data thus looks crap.

    – Ander Biguri
    Nov 21 '18 at 11:14











  • @AnderBiguri i actually managed to get it working but, it still doesn't loop

    – A Loli
    Nov 21 '18 at 17:22













  • So... you did not manage to get it working?

    – Ander Biguri
    Nov 21 '18 at 17:33











  • at this point yes, but it still doesn't loop, anyway this isn't my question anyway so this should be closed

    – A Loli
    Nov 22 '18 at 18:38








  • 1





    @AnderBiguri Yes. Gif format has a specific system where one colour index gets indicated as being "the transparent colour", and all pixels of that value are treated as transparent. In fact, there are more formats like that; png even supports alpha values for all of its palette indices, but .Net is equally dumb about that format and also treats it as ARGB.

    – Nyerguds
    Nov 28 '18 at 22:10



















I do not speak C#,but my best bet here is that you are missing the alpha channel. Note how the original gif has transparency but your output has no information about that. By not getting RGBA, and instead reading as RGB, you are reading random data thus looks crap.

– Ander Biguri
Nov 21 '18 at 11:14





I do not speak C#,but my best bet here is that you are missing the alpha channel. Note how the original gif has transparency but your output has no information about that. By not getting RGBA, and instead reading as RGB, you are reading random data thus looks crap.

– Ander Biguri
Nov 21 '18 at 11:14













@AnderBiguri i actually managed to get it working but, it still doesn't loop

– A Loli
Nov 21 '18 at 17:22







@AnderBiguri i actually managed to get it working but, it still doesn't loop

– A Loli
Nov 21 '18 at 17:22















So... you did not manage to get it working?

– Ander Biguri
Nov 21 '18 at 17:33





So... you did not manage to get it working?

– Ander Biguri
Nov 21 '18 at 17:33













at this point yes, but it still doesn't loop, anyway this isn't my question anyway so this should be closed

– A Loli
Nov 22 '18 at 18:38







at this point yes, but it still doesn't loop, anyway this isn't my question anyway so this should be closed

– A Loli
Nov 22 '18 at 18:38






1




1





@AnderBiguri Yes. Gif format has a specific system where one colour index gets indicated as being "the transparent colour", and all pixels of that value are treated as transparent. In fact, there are more formats like that; png even supports alpha values for all of its palette indices, but .Net is equally dumb about that format and also treats it as ARGB.

– Nyerguds
Nov 28 '18 at 22:10







@AnderBiguri Yes. Gif format has a specific system where one colour index gets indicated as being "the transparent colour", and all pixels of that value are treated as transparent. In fact, there are more formats like that; png even supports alpha values for all of its palette indices, but .Net is equally dumb about that format and also treats it as ARGB.

– Nyerguds
Nov 28 '18 at 22:10














0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53397255%2fgif-image-cropping-in-c-sharp-without-add-ons%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53397255%2fgif-image-cropping-in-c-sharp-without-add-ons%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

SQL update select statement

'app-layout' is not a known element: how to share Component with different Modules