locked
Using bytes[] in class to create .jpg image RRS feed

  • Question

  • User-122480877 posted

    Ok so I was unsure of what forum to post this, Sorry to the moderator, I had about 4 choices and thought perhaps you would know the better forum for this.

    What I am doing, Currently I have a class that will create an image either to file (using bytes) or will save bytes into the database.

    I allready modify and resize these images and everything is working great but as a request of users they would like a little protection with there images in regards to a copyright stamp with there user name in the lower right corner. I also propose to put another patern lets say watermark over the image as well. Perhaps the and image in the lower left.

    So this will be simple to someone with more image experience than myself and I welcome your ideas.

    I simply want to re-write the bytes to include this new overlay so to speak and want to have control on how transparent the new overlays are.

     

    Tuesday, May 5, 2009 8:48 AM

Answers

  • User-916962509 posted

    I never tried saving as jpg. Just try to make your code the same as mine, change it to ImageFormat.Png and then use the MemoryStream.WriteTo() to send it to the browser... That worked for me.

    Please have a try, if that works, maybe we can then try to work out why you can't do it as jpg...

    Regards
    bgs264

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 6, 2009 7:52 AM

All replies

  • User-916962509 posted

    This helped me to acheive the result, I hope it helps you too:
    http://www.codeproject.com/KB/GDI-plus/watermark.aspx

    Regards,
    bgs264

     

    Tuesday, May 5, 2009 9:08 AM
  • User-122480877 posted

    This helped me to acheive the result, I hope it helps you too:
    http://www.codeproject.com/KB/GDI-plus/watermark.aspx

    Regards,
    bgs264

    Thanks for trying to help this would work but I want to work with the bytes I have and I do not have the knowledge to create an Image in my class from the Byte Array I am allready using.

     I need to be able to extend and modify my current code.

    After my attempt to Import this idea into my project I could not find away to create an Image from a Byte Array so I searched the web and did not find anything of use apat from using Response.OutputStream.Write() and this a no go.

    Tuesday, May 5, 2009 11:18 AM
  • User-916962509 posted

    Try this to create an image from a byte array:

     
    Using ms As New MemoryStream(yourbytearray)
    Dim img As Image = Image.FromStream(ms)
    .......
    End Using
     

     

    Tuesday, May 5, 2009 11:23 AM
  • User-916962509 posted

     And here is what yu do when you're done working with it:

    (This is still within the context of the original Using block above)

     

     

                    Using stream As New MemoryStream
    img.Save(stream, ImageFormat.Png)
    stream.WriteTo(context.Response.OutputStream)
    End Using

      

    Thus, you end up with this:

     

     Using ms As New MemoryStream(yourbytearray)
    Dim img As Image = Image.FromStream(ms)
    Dim gfx As Graphics = Graphics.FromImage(img)

    ' Use gfx and img to do various manipulation and image trickery


    Using stream As New MemoryStream
    img.Save(stream, ImageFormat.Png)
    stream.WriteTo(context.Response.OutputStream)
    End Using
    End Using

    context.Response.Flush()
      
    Tuesday, May 5, 2009 11:26 AM
  • User-122480877 posted

    Wow allmost have some success with this but not quite.

    Have created and customized and tested to save to file,
    Images look great how ever I still need to get these images back and after spending most of the day trouble shooting and trying to find the problem it appears that I waill never actually find my problem as the eceptiondetails only tell me "A generic error occurred in GDI+."

    How ever I then decided to check my log files and this is what I have. Are you able to help?

    All I need is this Image to return as Byte[] [^o)]

     

    Log Entry : 05/06/2009 23:33:54

    -----------------------------------

    Error in : http://localhost:1053/Trading Center/System/Handler/PhotoDisplay.ashx?PhotoId=21585&Size=Medium.

    Error Message : System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.

    at System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder, EncoderParameters encoderParams)

    at System.Drawing.Image.Save(Stream stream, ImageFormat format)

    at AspNet.SpiderMaster.TradingCenter.Web.PhotoDisplay.ProcessRequest(HttpContext context) in d:\Spider Master\Visual Studio 2008\WebSites\Current Work\Trading Center\System\Handler\PhotoDisplay.ashx:line 49

    at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()

    at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

     

                        if (Photo != null) // this is Byte[]
                        {
                            //Byte[] PhotoOut = ImageManager.ApplyWaterMarkToImage(Photo);
                            Image ImageToConvert = ImageManager.ApplyWaterMarkToImage(Photo);
    
    
                            Byte[] PhotoOut;
                            using (MemoryStream ms = new MemoryStream())
                            {
                                ImageToConvert.Save(ms, ImageFormat.Jpeg); // Error is triggered from here
                                PhotoOut = ms.ToArray();
                            }
    
                            Response.ContentType = "image/jpeg";
                            Response.Cache.SetCacheability(HttpCacheability.Public);
                            Response.BufferOutput = false;
                            Response.OutputStream.Write(PhotoOut, 0, PhotoOut.Length);
                        }
     
    Wednesday, May 6, 2009 7:46 AM
  • User-916962509 posted

    I never tried saving as jpg. Just try to make your code the same as mine, change it to ImageFormat.Png and then use the MemoryStream.WriteTo() to send it to the browser... That worked for me.

    Please have a try, if that works, maybe we can then try to work out why you can't do it as jpg...

    Regards
    bgs264

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 6, 2009 7:52 AM