transparent component with aviwriter in aforge - c#

I want to make an avi video with aforge and I am trying to add bitmap frame with ARGB the A component (transparent component) is not working please is there any way to add transparent to aviwriter frame with aforge.net c# (even if not bitmap)
Bitmap my_video_image = new Bitmap(picturebox1.Image);
for (int i = 0; i < heigth11; i++)
{
for (int j = 0; j < width11; j++)
{
my_video_image.SetPixel(j,i,Color.FromArgb(trackBar4.Value,red,green, blue));
}
}
AVIWriter avi_Writer= new AVIWriter();
avi_Writer.Open("videoname.avi",600,600);
avi_Writer.AddFrame(my_video_image);
the avi WRiter added the image but without the tranparent component

Related

Can't convert tiff image to a bitmap (matrix)

The function GetBitMapColorMatrix is ā€¸supposed to decode the tiff image and return a bitmap that describes the tiff image but for some reason, I get this exception -> System.PlatformNotSupportedException: 'System.Drawing is not supported on this platform'. Any help would be appreciated.
public Color[][] GetBitMapColorMatrix(string bitmapFilePath)
{
Bitmap b1 = new Bitmap(bitmapFilePath);
int hight = b1.Height;
int width = b1.Width;
Color[][] colorMatrix = new Color[width][];
for (int i = 0; i < width; i++)
{
colorMatrix[i] = new Color[hight];
for (int j = 0; j < hight; j++)
{
colorMatrix[i][j] = b1.GetPixel(i, j);
}
}
return colorMatrix;
}
UWP does not use the full .net framework so the classes in System.Drawing are not available. Their are some classes like BitmapPallette and BitmapImage in the System.Media.Imaging namespace that could be useful. I would also look at Win2D or SharpDX.

How can I remove transparency from an animated GIF?

I can't believe this hasn't been asked yet, but...
How can I replace the transparent background of a GIF image to white, without breaking the animation?
I am way out of my field here, so I'm pretty much lost (I really don't know what to do).
This is what I tried without luck:
public byte[] RemoveTransparency(byte[] gifBytes)
{
using(MemoryStream gifStream = new MemoryStream(gifBytes))
using(Image gifImage = Image.FromStream(gifStream))
using(Image newImage = new Bitmap(gifImage.Width, gifImage.Height, PixelFormat.Format32bppPArgb))
using(MemoryStream newImageStream = new MemoryStream())
{
Color background = Color.White;
newImage.SetResolution(gifImage.HorizontalResolution, gifImage.VerticalResolution);
using (Graphics graphics = Graphics.FromImage(newImage))
{
graphics.Clear(background );
graphics.DrawImage(gifImage, 0, 0, gifImage.Width, gifImage.Height);
}
newImage.Save(newImageStream);
return newImageStream.ToArray();
}
}
You can use a bitmap.
using(var ms = new MemoryStream(gifBytes))
using(var b = new Bitmap(Image.FromStream(ms)))
{
for(var y = 0; y < b.Height; ++y)
for(var x = 0; x < b.Width; ++x)
if(b.GetPixel(x, y).A == 0) //Completely opaque
b.SetPixel(x, y, Color.White); //Sets white
}
//Note: b goes out of scope here and will be deleted eventually.
Do note that this algorithm is extremely slow on large images, a typical approach to get around this is to access the internal buffer directly which requires unsafe context. See MSDN Bitmap reference or this: https://msdn.microsoft.com/en-us/library/5ey6h79d(v=vs.110).aspx
If the bitmap breaks the animation, load each frame as described in the comments of your post and then write the final buffer back into main image memory. Comment if you want me to edit for this context.

How to identify the image is greyscale image or color image in c#

I have requirement of validating a bulk number of images (jpg, tif, png) in a folder with height attributes. but the validation rules are different for color images and grayscale images.
but My Problem is
How to identify the image is grayscale image or color image in c#?
at least where to start?
bool IsGreyScale(Bitmap YourCurrentBitmap)
{
Color c;
for(int i=0; i < YourCurrentBitmap.Width; i++)
for(int j=0; j < YourCurrentBitmap.Height; j++)
{
c = YourCurrentBitmap.GetPixel(i,j);
if(!(c.R == c.G == c.B)) return false;
}
return true;
}
But this method is relatively slow though.

How to save image in indexed format and get it's palette ?

I want to save image as Format8bppIndexed using this code :
Bitmap imgsource = new Bitmap(sourceimage);
Bitmap imgtarget = new Bitmap(imgsource.Width, imgsource.Height, PixelFormat.Format8bppIndexed);
for (int I = 0; I <= imgsource.Width - 1; I++)
{
for (int J = 0; J <= imgsource.Height - 1; J++)
{
imgtarget.SetPixel(I, J, imgsource.GetPixel(I, J));
}
}
imgtarget.Save(targetimage);
but I face error that "Setpixel is not supported for images with indexed pixel formats"
and I want to save image with indexed
how I can do that ?
Use this instead:
Bitmap imgtarget = imgsource.Clone(
new Rectangle(0, 0, imgsource.Width, imgsource.Height),
PixelFormat.Format8bppIndexed);
EDIT:
There are two kind of Images in GDI+:
Bitmaps and Metafiles. Usually you load the image from a bitmap image file (.jpg, .png, .bmp, .gif, .exif and .tiff) and not a metafile (.wmf or .emf). So, instead of creating a new bitmap based on the image, just cast the Image object to Bitmap:
Bitmap imgsource = (Bitmap)sourceimage;
The first line of your code, changes the origianl properties of the image and resets the DIP to 96.

How do I superimpose one bitmap image on another in GDI+?

Using GDI+ I've made a heatmap bmp and I'd like to superimpose it on top of my bmp map. I've saved the two bmps to disk, and they look good, I just need a way to put them together. Is there any way to do this, perhaps using the Graphics object? How is transparency/alpa involved?
I'm very new to GDI programming so please be as specific as possible.
OK - here's an answer. At some point I need to learn how GDI+ works...
I couldn't get around the transarency issues, but this works. It just copies the non-white pixels from the overlay to the map:
for (int x = 0; x < map.Width; x++)
for (int y = 0; y < map.Height; y++) {
Color c = overlay.GetPixel(x, y);
if ((c.A != 255) || (c.B != 255) || (c.G != 255) || (c.R != 255))
map.SetPixel(x, y, c);
This should do the job...
At the moment the Image you want to superimpose onto the main image will be located in the top left corner of the main Image, hence the new Point(0,0). However you could change this to locate the image anywhere you want.
void SuperimposeImage()
{
//load both images
Image mainImage = Bitmap.FromFile("PathOfImageGoesHere");
Image imposeImage = Bitmap.FromFile("PathOfImageGoesHere");
//create graphics from main image
using (Graphics g = Graphics.FromImage(mainImage))
{
//draw other image on top of main Image
g.DrawImage(imposeImage, new Point(0, 0));
//save new image
mainImage.Save("OutputFileName");
}
}

Categories

Resources