Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

combine mutliple images to avi

42 views
Skip to first unread message

Roland Wolters

unread,
Feb 27, 2003, 5:37:21 AM2/27/03
to
Hi,

I want to use my webcam as security camera. To do this I
poll an image from the cam every say 15 secs. These Images
get stored on the harddrive. Viewing them over a day or
so, is a problem. So i want to build a program that puts
all these images together in a avi or wathever filetype I
can view in mediaplayer.

Is there an easy way to do this the dot-net way?

Greetings,

Roland

Nicholas Paldino [.NET/C# MVP]

unread,
Feb 27, 2003, 9:02:28 AM2/27/03
to
Roland,

There is not an easy way to do this, or a .NET way to do this because
the framework doesn't come with multimedia capabilities out of the box. My
recommendation would be to see if there is a third party component that you
can use (through .NET or interop), or better yet, see if Movie Maker by MS
has an API that you can tap into through interop.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- nicholas...@exisconsulting.com

"Roland Wolters" <rol...@wolters-ict.nl> wrote in message
news:01de01c2de4c$359fefb0$a001...@phx.gbl...

Rene Nyffenegger

unread,
Mar 3, 2003, 12:03:10 PM3/3/03
to


Hello Roland

I have written a c# Programm that can write/create AVI files. I post it in
the hope you'll find it useful. Basically, it works as follows:
1) you create an AviWriter instance.
2) You call Open. This creates the file and returns a Bitmap.
3) You paint on the Bitmap
4) You call Write on the AviWriter instance. This adds a frame to the
movie
5) Repeat 3,4
6) when finished, call Close()


This example renders Mandelbrot images. Each image is a little more zoomed in,
so that you get a cool visual effect.

using System;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Drawing.Imaging;

// vfw.h
[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct AVISTREAMINFOW {
public UInt32 fccType;
public UInt32 fccHandler;
public UInt32 dwFlags;
public UInt32 dwCaps;
public UInt16 wPriority;
public UInt16 wLanguage;
public UInt32 dwScale;
public UInt32 dwRate;
public UInt32 dwStart;
public UInt32 dwLength;
public UInt32 dwInitialFrames;
public UInt32 dwSuggestedBufferSize;
public UInt32 dwQuality;
public UInt32 dwSampleSize;
public UInt32 rect_left;
public UInt32 rect_top;
public UInt32 rect_right;
public UInt32 rect_bottom;
public UInt32 dwEditCount;
public UInt32 dwFormatChangeCount;
public UInt16 szName0;
public UInt16 szName1;
public UInt16 szName2;
public UInt16 szName3;
public UInt16 szName4;
public UInt16 szName5;
public UInt16 szName6;
public UInt16 szName7;
public UInt16 szName8;
public UInt16 szName9;
public UInt16 szName10;
public UInt16 szName11;
public UInt16 szName12;
public UInt16 szName13;
public UInt16 szName14;
public UInt16 szName15;
public UInt16 szName16;
public UInt16 szName17;
public UInt16 szName18;
public UInt16 szName19;
public UInt16 szName20;
public UInt16 szName21;
public UInt16 szName22;
public UInt16 szName23;
public UInt16 szName24;
public UInt16 szName25;
public UInt16 szName26;
public UInt16 szName27;
public UInt16 szName28;
public UInt16 szName29;
public UInt16 szName30;
public UInt16 szName31;
public UInt16 szName32;
public UInt16 szName33;
public UInt16 szName34;
public UInt16 szName35;
public UInt16 szName36;
public UInt16 szName37;
public UInt16 szName38;
public UInt16 szName39;
public UInt16 szName40;
public UInt16 szName41;
public UInt16 szName42;
public UInt16 szName43;
public UInt16 szName44;
public UInt16 szName45;
public UInt16 szName46;
public UInt16 szName47;
public UInt16 szName48;
public UInt16 szName49;
public UInt16 szName50;
public UInt16 szName51;
public UInt16 szName52;
public UInt16 szName53;
public UInt16 szName54;
public UInt16 szName55;
public UInt16 szName56;
public UInt16 szName57;
public UInt16 szName58;
public UInt16 szName59;
public UInt16 szName60;
public UInt16 szName61;
public UInt16 szName62;
public UInt16 szName63;
}

[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct AVICOMPRESSOPTIONS {
public UInt32 fccType;
public UInt32 fccHandler;
public UInt32 dwKeyFrameEvery; // only used with AVICOMRPESSF_KEYFRAMES
public UInt32 dwQuality;
public UInt32 dwBytesPerSecond; // only used with AVICOMPRESSF_DATARATE
public UInt32 dwFlags;
public IntPtr lpFormat;
public UInt32 cbFormat;
public IntPtr lpParms;
public UInt32 cbParms;
public UInt32 dwInterleaveEvery;
}

[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct BITMAPINFOHEADER {
public UInt32 biSize;
public Int32 biWidth;
public Int32 biHeight;
public Int16 biPlanes;
public Int16 biBitCount;
public UInt32 biCompression;
public UInt32 biSizeImage;
public Int32 biXPelsPerMeter;
public Int32 biYPelsPerMeter;
public UInt32 biClrUsed;
public UInt32 biClrImportant;
}

public class AviException : ApplicationException {
public AviException(string s) : base(s) {}
}

public class AviWriter {


public Bitmap Open(string fileName, UInt32 frameRate, int width, int height) {
frameRate_ = frameRate;
width_ = (UInt32) width;
height_ = (UInt32) height;
bmp_ = new Bitmap(width,height,PixelFormat.Format24bppRgb);
BitmapData bmpDat = bmp_.LockBits(
new Rectangle(0,0,width, height),
ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
stride_ = (UInt32) bmpDat.Stride;
bmp_.UnlockBits(bmpDat);
AVIFileInit();
int hr = AVIFileOpenW(
ref pfile_, fileName,
4097 /* OF_WRITE | OF_CREATE (winbase.h) */, 0);
if (hr != 0) {
throw new AviException("error for AVIFileOpenW");
}
return bmp_;
}

public void AddFrame() {
if (count_ == 0) {
CreateStream();
SetOptions();
}

BitmapData bmpDat = bmp_.LockBits(
new Rectangle(
0,0,(int) width_, (int) height_),
ImageLockMode.ReadOnly,PixelFormat.Format24bppRgb);

int hr= AVIStreamWrite(psCompressed_, count_, 1,
bmpDat.Scan0, // pointer to data
(Int32) (stride_ * height_),
0, //16, // 16 = AVIIF_KEYFRAMe
0,
0);

if (hr != 0) {
throw new AviException("AVIStreamWrite");
}

bmp_.UnlockBits(bmpDat);

count_ ++;
}

public void Close() {
AVIStreamRelease(ps_);
AVIStreamRelease(psCompressed_);

AVIFileRelease(pfile_);
AVIFileExit();
}

private void CreateStream() {
AVISTREAMINFOW strhdr = new AVISTREAMINFOW();
strhdr.fccType = fccType_;
strhdr.fccHandler = fccHandler_;
strhdr.dwFlags = 0;
strhdr.dwCaps = 0;
strhdr.wPriority = 0;
strhdr.wLanguage = 0;
strhdr.dwScale = 1;
strhdr.dwRate = frameRate_; // Frames per Second
strhdr.dwStart = 0;
strhdr.dwLength = 0;
strhdr.dwInitialFrames = 0;
strhdr.dwSuggestedBufferSize = height_ * stride_;
strhdr.dwQuality = 0xffffffff; //-1; // Use default
strhdr.dwSampleSize = 0;
strhdr.rect_top = 0;
strhdr.rect_left = 0;
strhdr.rect_bottom = height_;
strhdr.rect_right = width_;
strhdr.dwEditCount = 0;
strhdr.dwFormatChangeCount = 0;
strhdr.szName0 = 0;
strhdr.szName1 = 0;

int hr = AVIFileCreateStream(pfile_, out ps_, ref strhdr);

if (hr != 0) {
throw new AviException("AVIFileCreateStream");
}
}

private void SetOptions() {
AVICOMPRESSOPTIONS opts = new AVICOMPRESSOPTIONS();
opts.fccType = fccType_;
opts.fccHandler = fccHandler_;
opts.dwKeyFrameEvery = 0;
opts.dwQuality = 0; // 0 .. 10000
opts.dwFlags = 0; // AVICOMRPESSF_KEYFRAMES = 4
opts.dwBytesPerSecond= 0;
opts.lpFormat = new IntPtr(0);
opts.cbFormat = 0;
opts.lpParms = new IntPtr(0);
opts.cbParms = 0;
opts.dwInterleaveEvery = 0;

int hr = AVIMakeCompressedStream(out psCompressed_, ps_, ref opts, 0);
if (hr != 0) {
throw new AviException("AVIMakeCompressedStream");
}

BITMAPINFOHEADER bi = new BITMAPINFOHEADER();
bi.biSize = 40;
bi.biWidth = (Int32) width_;
bi.biHeight = (Int32) height_;
bi.biPlanes = 1;
bi.biBitCount = 24;
bi.biSizeImage = stride_*height_;

hr = AVIStreamSetFormat(psCompressed_, 0, ref bi, 40);
if (hr != 0) {
throw new AviException("AVIStreamSetFormat");
}
}

[DllImport("avifil32.dll")]
public static extern void AVIFileInit();

[DllImport("avifil32.dll")]
public static extern int AVIFileOpenW(
ref int ptr_pfile,
[MarshalAs(UnmanagedType.LPWStr)]string fileName,int flags, int dummy);

[DllImport("avifil32.dll")]
public static extern int AVIFileCreateStream(
int ptr_pfile, out IntPtr ptr_ptr_avi,
ref AVISTREAMINFOW ptr_streaminfo);
[DllImport("avifil32.dll")]
public static extern int AVIMakeCompressedStream(
out IntPtr ppsCompressed, IntPtr aviStream,
ref AVICOMPRESSOPTIONS ao, int dummy);

[DllImport("avifil32.dll")]
public static extern int AVIStreamSetFormat(
IntPtr aviStream, Int32 lPos,
ref BITMAPINFOHEADER lpFormat, Int32 cbFormat);

[DllImport("avifil32.dll")]
public static extern int AVIStreamWrite(
IntPtr aviStream, Int32 lStart, Int32 lSamples,
IntPtr lpBuffer, Int32 cbBuffer, Int32 dwFlags,
Int32 dummy1, Int32 dummy2);

[DllImport("avifil32.dll")]
public static extern int AVIStreamRelease(IntPtr aviStream);

[DllImport("avifil32.dll")]
public static extern int AVIFileRelease(int pfile);

[DllImport("avifil32.dll")]
public static extern void AVIFileExit();

private int pfile_ = 0;
private IntPtr ps_ = new IntPtr(0);
private IntPtr psCompressed_ = new IntPtr(0);
private UInt32 frameRate_ = 0;
private int count_ = 0;
private UInt32 width_ = 0;
private UInt32 stride_ = 0;
private UInt32 height_ = 0;
private UInt32 fccType_ = 1935960438; // vids
private UInt32 fccHandler_ = 1145656899; // CVID

private Bitmap bmp_;
};

public struct Complex {
public Complex(double re_, double im_) {
re = re_;
im = im_;
}
public static Complex operator +(Complex arg1, Complex arg2) {
return (new Complex(arg1.re + arg2.re, arg1.im + arg2.im));
}

public static Complex operator -(Complex arg1) {
return (new Complex(-arg1.re, -arg1.im));
}

public static Complex operator -(Complex arg1, Complex arg2) {
return (new Complex(arg1.re - arg2.re, arg1.im - arg2.im));
}

public static Complex operator *(Complex arg1, Complex arg2) {
return (new Complex(
arg1.re * arg2.re - arg1.im * arg2.im,
arg1.re * arg2.im + arg2.re * arg1.im));
}

public static Complex operator /(Complex arg1, Complex arg2) {
double c1, c2, d;
d = arg2.re * arg2.re + arg2.im * arg2.im;
if (d == 0) {
return (new Complex(0, 0));
}
c1 = arg1.re * arg2.re + arg1.im * arg2.im;
c2 = arg1.im * arg2.re - arg1.re * arg2.im;
return (new Complex(c1 / d, c2 / d));
}

public double Abs() {
return (Math.Sqrt(re * re + im * im));
}

public double Arg() {
double ret = 0;
if (re != 0)
ret = (180 / Math.PI) * Math.Atan(im / re);
return (ret);
}

public override string ToString() {
return (String.Format("Complex: ({0}, {1})", re, im));
}

private double re;
private double im;
}


class MandelBrot {
static public void CalcMandelBrot(
Bitmap bmp,
double xCoordFrom, double yCoordFrom,
double xCoordTo, double yCoordTo,
Color[] palette) {
int width =bmp.Width;
int height=bmp.Height;

int iterMax = palette.Length;

for (int x=0; x<width; x++) {
double xCoord=xCoordFrom+(xCoordTo-xCoordFrom)/width*x;
for (int y=0; y<height; y++) {
double yCoord=yCoordFrom+(yCoordTo-yCoordFrom)/height*y;

int cnt=0;

Complex c = new Complex(xCoord,yCoord);
Complex z = new Complex( 0, 0);
Complex z2= new Complex( 0, 0);
while ((z2.Abs()<2.0) && (cnt < iterMax)) {
z = z2 + c;
z2=z*z;
cnt++;
}
if (cnt == iterMax) {
bmp.SetPixel(x,y,Color.FromArgb(0,0,0));
}
else {
bmp.SetPixel(x,y,palette[cnt]);
}
}
}
}
}

class M {
public static void Main() {
try {
Color[] palette = new Color[50];
for (int i=0; i<50;i++) {
palette[i]=Color.FromArgb(i*4,255-i*4,50+i*2);
}
int w = 600;
int h = 600;
AviWriter aw = new AviWriter();
Bitmap bmp = aw.Open("test.avi", 25,w,h);

double f = 1.2;
double centerX = -0.7454333;
double centerY = -0.1130211;
double pctAreaNewImage = 0.9;
double endWidth_times_2 = 0.0001;

while (f>endWidth_times_2) {

MandelBrot.CalcMandelBrot(
bmp,
centerX-f,centerY-f,
centerX+f,centerY+f,
palette);

f=Math.Sqrt(pctAreaNewImage*f*f);
aw.AddFrame();
Console.Write(".");
}

aw.Close();
}
catch (AviException e) {
Console.WriteLine("AVI Exception in: " + e.ToString());
}
}
}

hth
Rene Nyffenegger


--
Projektleitung und Entwicklung in Oracle/C++/C# Projekten
http://www.adp-gmbh.ch/cv.html

0 new messages