|
Hi,
With images from files or from a database I don't have problems but when I work with images created starting from an object "Graphics" the images don't survive. How can I save this Bitmap?, I tried with:
...
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap( widthBmp, heightBmp );
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage( bmp );
...
MemoryStream stream = new MemoryStream();
bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] buffer = new Byte[stream.Length];
stream.Position = 0;
stream.Read(buffer, 0, (int) buffer.Length);
...
buffer = null;
stream.Close();
but the image always is black...
Thanks a lot
Abel Castillo
|
|
|
|
|
Hey there,
I think there are a few problems in your code but I'm not 100% sure. First of all, your sample here doesn't really draw anyhting on the bitmap so - it should be black. Second of all, if you want to save the bitmap you will most likely need a FileStream, not a MemoryStream. The MemoryStream is just an in-memory stream. It will not be persisted anywhere.
Maybe if you provide a bit more of your code here we'll be able to help out.
Cheers
----
www.digitalGetto.com
|
|
|
|
|
The image is drawn in another method ("DrawGraphics") but I didn't include it because it is very long
this it is a summary of the code:
public System.Drawing.Bitmap CreateBitmap( )
{
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap( widthBmp, heightBmp );
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage( bmp );
DrawGraphics( g, new System.Drawing.Point( 0, 0 ) );
g.Dispose( );
return bmp;
}
System.Drawing.Bitmap bmp = CreateBitmap();
// make a memory stream to work with the image bytes
MemoryStream stream = new MemoryStream();
// put the image into the memory stream
bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
// make byte array the same size as the image
byte[] buffer = new Byte[stream.Length];
// rewind the memory stream
stream.Position = 0;
// load the byte array with the image
stream.Read(buffer, 0, (int) buffer.Length);
then
save the image in sql database, but the array of bytes (variable buffer) has no images, it´s totally black...
Thank you for the answer
Abel Castillo
|
|
|
|
|
I'd say the problem is in the way you are creating the bitmap, and not how you save it into the MemoryStream. Josh is right, about not saving to a file but from your post I understand that it is not your intention to save to a file but rather to store the image bytes in a SQL database.
I think the problem is in the CreateBitmap() function. I suspect it has to do with the fact that you create a graphics context from a bitmap that is empty. Than you draw to the graphics object but then dispose of it before saving. I'm not sure if that is what's going on but before you try saving it, draw it on a canvas on the screen to make sure the bitmap is properly created by CreateBitmap()
----
www.digitalGetto.com
|
|
|
|
|
I agree with you, I have no problems to save the image to a SQL database and to show it in a PictureBox. But when I tried to save in a array of bytes ...
I'm working with the code of the excellent article Creating EAN-13 Barcodes with C#[^] by rainman_63. I generate the barcodes of several articles and I need to pass them to a database SQL, I have carried out several tests but up to now without success.
Ean13 ean13 = new Ean13();<br />
ean13.CountryCode = "84";<br />
ean13.ManufacturerCode = "862";<br />
ean13.ProductCode = "3274243";<br />
ean13.ChecksumDigit = "7";<br />
ean13.Scale = 1f;<br />
Bitmap bmp = ean13.CreateBitmap();<br />
<br />
MemoryStream stream = new MemoryStream();<br />
bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);<br />
byte[] buffer = new Byte[stream.Length];<br />
stream.Position = 0;<br />
stream.Read(buffer, 0, (int) buffer.Length);
here save barcodes (buffer) in a SQL database
buffer = null;<br />
stream.Close();
Thanks a lot
Abel Castillo
-- modified at 5:16 Thursday 22nd June, 2006
|
|
|
|
|
|
I don't know what is not working in your application but here is code that I wrote to verify your concept and it all works. To me, the problem is in your CreateBitmap() function.
To test the following code, create a new C# Windows Apllication. Drag and Drop two picture boxes on your form. Then paste this code as your Form Load event handler:
<code language="c#">
private void Form1_Load(object sender, EventArgs e)
{
MemoryStream memStream = new MemoryStream();
byte[] myBytes;
using (Bitmap bmp = new Bitmap(50, 50))
using (Graphics grfx = Graphics.FromImage(bmp))
{
grfx.FillRectangle(Brushes.Red, new Rectangle(0, 0, 50, 50));
bmp.Save(memStream, System.Drawing.Imaging.ImageFormat.Bmp);
myBytes = new byte[memStream.Length];
memStream.Position = 0;
memStream.Read(myBytes, 0, (int)memStream.Length);
pictureBox1.Image = Image.FromStream(memStream);
pictureBox2.Image = Image.FromStream(new MemoryStream(myBytes));
}
}
</code>
*** Portion of the code above is taken from the post below[^]
----
www.digitalGetto.com
-- modified at 12:38 Thursday 22nd June, 2006
|
|
|
|
|
Hello mikanu,
This is the code:
public System.Drawing.Bitmap CreateBitmap( )
{
float tempWidth = ( this.Width * this.Scale ) * 100 ;
float tempHeight = ( this.Height * this.Scale ) * 100;
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap( (int)tempWidth, (int)tempHeight );
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage( bmp );
this.DrawEan13Barcode( g, new System.Drawing.Point( 0, 0 ) );
g.Dispose( );
return bmp;
}
public void DrawEan13Barcode( System.Drawing.Graphics g, System.Drawing.Point pt )
{
float width = this.Width * this.Scale;
float height = this.Height * this.Scale;
float lineWidth = width / 113f;
System.Drawing.Drawing2D.GraphicsState gs = g.Save( );
g.PageUnit = System.Drawing.GraphicsUnit.Millimeter;
g.PageScale = 1;
System.Drawing.SolidBrush brush = new System.Drawing.SolidBrush( System.Drawing.Color.Black );
float xPosition = 0;
System.Text.StringBuilder strbEAN13 = new System.Text.StringBuilder( );
System.Text.StringBuilder sbTemp = new System.Text.StringBuilder( );
float xStart = pt.X;
float yStart = pt.Y;
float xEnd = 0;
System.Drawing.Font font = new System.Drawing.Font( "Arial", this._fFontSize * this.Scale );
this.CalculateChecksumDigit( );
sbTemp.AppendFormat( "{0}{1}{2}{3}",
this.CountryCode,
this.ManufacturerCode,
this.ProductCode,
this.ChecksumDigit );
string sTemp = sbTemp.ToString( );
string sLeftPattern = "";
sLeftPattern = ConvertLeftPattern( sTemp.Substring( 0, 7 ) );
strbEAN13.AppendFormat( "{0}{1}{2}{3}{4}{1}{0}",
this._sQuiteZone, this._sLeadTail,
sLeftPattern,
this._sSeparator,
ConvertToDigitPatterns( sTemp.Substring( 7 ), this._aRight ) );
string sTempUPC = strbEAN13.ToString( );
float fTextHeight = g.MeasureString( sTempUPC, font ).Height;
for( int i = 0; i < strbEAN13.Length; i++ )
{
if( sTempUPC.Substring( i, 1 ) == "1" )
{
if( xStart == pt.X )
xStart = xPosition;
if( ( i > 12 && i < 55 ) || ( i > 57 && i < 101 ) )
g.FillRectangle( brush, xPosition, yStart, lineWidth, height - fTextHeight );
else
g.FillRectangle( brush, xPosition, yStart, lineWidth, height );
}
xPosition += lineWidth;
xEnd = xPosition;
}
xPosition = xStart - g.MeasureString( this.CountryCode.Substring( 0, 1 ), font ).Width;
float yPosition = yStart + ( height - fTextHeight );
g.DrawString( sTemp.Substring( 0, 1 ), font, brush, new System.Drawing.PointF( xPosition, yPosition ) );
xPosition += ( g.MeasureString( sTemp.Substring( 0, 1 ), font ).Width + 43 * lineWidth ) -
( g.MeasureString( sTemp.Substring( 1, 6 ), font ).Width );
g.DrawString( sTemp.Substring( 1, 6 ), font, brush, new System.Drawing.PointF( xPosition, yPosition ) );
xPosition += g.MeasureString( sTemp.Substring( 1, 6 ), font ).Width + ( 11 * lineWidth );
g.DrawString( sTemp.Substring( 7 ), font, brush, new System.Drawing.PointF( xPosition, yPosition ) );
g.Restore( gs );
}
the difference would be the code of the function DrawEan13Barcode, but I don't know why...
I have carried out many tests, but when I use the graph that returns this function I can show it in a PictureBox but never to save it successfully in a stream.
Excuses my insistence, thanks for your time
Abel Castillo
|
|
|
|
|
The only difference that I could think of it that you dispose of the Graphics object in the CreateBitmap function whereas I created the MemoryStream before disposing the Graphics object. Give that a try.. I don't have a lot of time on my hands right now to test, but I'd be intereseted to know if that fixes it.
Miky
----
www.digitalGetto.com
|
|
|
|
|
Thanks Miky,
I'm making other tests and contacting with other programmers, when I find the solution I publish it.
cheers Abel
Abel Castillo
|
|
|
|
|
This works for me:
private void button1_Click(object sender, System.EventArgs e)
{
using( Bitmap bmp = new Bitmap( 50, 50 ) )
using( Graphics grfx = Graphics.FromImage( bmp ) )
{
grfx.FillRectangle( Brushes.Red, new Rectangle(0,0,50,50) );
using( FileStream stream = new FileStream( @"C:\test.jpg", FileMode.Create ) )
{
bmp.Save( stream, System.Drawing.Imaging.ImageFormat.Jpeg );
}
}
}
I think the difference between our code is that I am saving to a FileStream, but you are saving to a MemoryStream. Your stream does not send the image to a file.
Josh
|
|
|
|
|
hello all
i'm new to the world of directX and directShow.
i need to draw shapes on video.
does anyone know how to do that? where should i begin?
thanks
eyal
|
|
|
|
|
I would think that you could get the device context for whatever window the video is playing in and draw on there as normal - however, don't quote me on this.
My Music | My Pics | My Articles
BlackDice
|
|
|
|
|
Last painter wins... and around the back stretch it's DirectX by a nose!
|
|
|
|
|
|
DirectShow does all the rendering of the video media correct? Even if you got the handle to the device context it is using and try to draw on it you just have a race condition (last painter wins) don't you?
|
|
|
|
|
so what is the best way to do it?
|
|
|
|
|
I suppose you could z-order windows so your new shapes are actually in a different window on top of the window that the video is renedered in. Just wild thoughts... I have no experience doing anything with DirectShow.
Another approach could be altering the media stream content to contain the new shapes.
|
|
|
|
|
Hi,
I'm struggling with the following problem:
I have a large unmanaged buffer, allocated with VirtualAlloc for performmance reasons. I use this buffer to load prepared bitmap image data into it.
For displaying, I tried to instantiate a bitmap object the following way:
<br />
<br />
<br />
Bitmap bmp = new Bitmap(Width, Height, Width * 4, System.Drawing.Imaging.PixelFormat.Format32bppPArgb, new IntPtr((void*)BufPtrStart.ToPointer());<br />
<br />
Usually, as I understood, creating a bitmap this way should not allocate any memory through the GC. But in my tests I found that memory is allocated anyway. Interestingly it isn't the size the bitmap has uncompressed in memory, but it is a significant amount.
Does anybody have similar Effects and knows the cause ?
Bye,
Florian
-- modified at 11:45 Wednesday 21st June, 2006
|
|
|
|
|
I'm writing an app that will talk to a USB device. I wonder how can I check that i have lost my USB connection just like in windows.
Donkaiser
|
|
|
|
|
Hi,
I tried to send mail from c# windows application but i am unable to send the mail.i am getting the following error.
An unhandled exception of type 'System.Web.HttpException' occurred in system.web.dll
Additional information: Could not access 'CDO.Message' object.
The code i wrote is
System.Web.Mail.MailMessage mailMsg = new MailMessage();
mailMsg.From ="chand.p@icinfotech.com";
mailMsg.To = "chand.p@icinfotech.com";
mailMsg.Priority = MailPriority.High;
mailMsg.Subject = subject;
mailMsg.Body = body;
mailMsg.BodyFormat = System.Web.Mail.MailFormat.Html;
MailAttachment sFile = new MailAttachment(attachment);
mailMsg.Attachments.Add(sFile);
SmtpMail.SmtpServer = "127.0.0.1";
SmtpMail.Send(mailMsg);
reply me if any one know the solution for this problem.
|
|
|
|
|
Unless your local machine is the mail server you will need to set SmtpServer to a valid address, maybe something like mail.mycompany.com or 234.10.44.20
|
|
|
|
|
hi thanks for replying me.
i have used my company mail server but i am receiving the same error.i tried this in vs2003 but in vs2005 it is working fine.
|
|
|
|
|
If you are using VS2005 you should use the System.Net.Mail namespace, System.Web.Mail has been deprecated.
|
|
|
|
|
Yes,i used System.Net.Mail namespace in vs2005.Do you have any alternate solution for my problem?if any,please kindly reply.
|
|
|
|
|