1

This function return an resized and centered image. I would like tu execute it like thumb.aspx?image=test.jpg&width=100&height=50&needToFill=tru‌​e to get a ContentType = "image/jpeg"

But I get an error on return bmPhoto Page_Load(object, System.EventArgs) is null

Note

I knew that this code is wrong, but I would like to understand how to execute the same function in buffer.

<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Drawing2D" %>
<script language="C#" runat="server">
void Page_Load(Object sender, EventArgs e) {
    Response.Cache.VaryByParams["Image;Width;Height;needToFill"] = true;
    Response.ContentType = "image/jpeg";
    string imageLocation = Server.MapPath(Request.QueryString["Image"]);
    int Width = Convert.ToInt32(Request.QueryString["Width"]);
    int Height = Convert.ToInt32(Request.QueryString["Height"]);
    System.Drawing.Bitmap image = new System.Drawing.Bitmap(imageLocation);
    int sourceWidth = image.Width;
    int sourceHeight = image.Height;
    int sourceX = 0;
    int sourceY = 0;
    double destX = 0;
    double destY = 0;
    double nScale = 0;
    double nScaleW = 0;
    double nScaleH = 0;
    bool needToFill=true;
    nScaleW = ((double)Width / (double)sourceWidth);
    nScaleH = ((double)Height / (double)sourceHeight);

    if (Request.QueryString["needToFill"] != null) {
        needToFill = Convert.ToBoolean(Request.QueryString["needToFill"]);
    }

    if (!needToFill) {
        nScale = Math.Min(nScaleH, nScaleW);
    } else {
        nScale = Math.Max(nScaleH, nScaleW);
        destY = (Height - sourceHeight * nScale) / 2;
        destX = (Width - sourceWidth * nScale) / 2;
    }

    if (nScale > 1) nScale = 1;

    int destWidth = (int)Math.Round(sourceWidth * nScale);
    int destHeight = (int)Math.Round(sourceHeight * nScale);

    System.Drawing.Bitmap bmPhoto = null;
    try {
        bmPhoto = new System.Drawing.Bitmap(destWidth + (int)Math.Round(2 * destX), destHeight + (int)Math.Round(2 * destY));
    }
    catch (Exception ex) {
        throw new ApplicationException(string.Format("destWidth:{0}, destX:{1}, destHeight:{2}, desxtY:{3}, Width:{4}, Height:{5}",
            destWidth, destX, destHeight, destY, Width, Height), ex);
    }
    using (System.Drawing.Graphics grPhoto = System.Drawing.Graphics.FromImage(bmPhoto)) {
        grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
        grPhoto.CompositingQuality = CompositingQuality.HighQuality;
        grPhoto.SmoothingMode = SmoothingMode.HighQuality;
        Rectangle to =  new System.Drawing.Rectangle((int)Math.Round(destX), (int)Math.Round(destY), destWidth, destHeight);
        Rectangle from = new System.Drawing.Rectangle(sourceX, sourceY, sourceWidth, sourceHeight);
        grPhoto.DrawImage(image, to, from, System.Drawing.GraphicsUnit.Pixel);
        return bmPhoto;
    }
}
</script>
Vixed
  • 3,429
  • 5
  • 37
  • 68
  • you cannot return something on a method of type void. Have you tried to write the image to the reponse stream? – Juergen Gutsch Nov 17 '16 at 12:18
  • no, because I'm not a .net dev. So I realy don't know how to move. @JuergenGutsch – Vixed Nov 17 '16 at 12:22
  • https://blogs.msdn.microsoft.com/laurelle/2011/11/15/writing-a-generic-asp-net-handler-to-return-an-image-and-control-the-image/ – Juergen Gutsch Nov 17 '16 at 12:32
  • The `Page_Load()` event handler is a `void` procedure it doesn't expect a `return` type. Why would you use `Page_Load()` for a process that has no HTML returned? – user692942 Nov 17 '16 at 12:32
  • @Lankymart i.e. my answer – TheLethalCoder Nov 17 '16 at 12:34
  • @Lankymart Itried using **bmPhoto.Save(Response.OutputStream, outputFormat);** instead of return, but nothing – Vixed Nov 17 '16 at 12:35
  • 1
    @TheLethalCoder yeah, was busy editing the question...this OPs formatting doesn't get any better, however many times I edit their questions. – user692942 Nov 17 '16 at 12:35
  • You might want to use an [ashx](http://stackoverflow.com/questions/2253228/asp-net-return-image-from-aspx-link) instead – Manfred Radlwimmer Nov 17 '16 at 12:36
  • Your confusing displaying an image in a ASP.Net control with returning a binary response from a ASP.Net page to populate a HTML image. – user692942 Nov 17 '16 at 12:36
  • 2
    Possible duplicate of [How can I execute an aspx from url returning an ContentType Image?](http://stackoverflow.com/questions/40636439/how-can-i-execute-an-aspx-from-url-returning-an-contenttype-image) – user692942 Nov 17 '16 at 12:42
  • You already got an [extensive answer](http://stackoverflow.com/a/40637495/692942) why have you posted again? Please review [ask] before posting, this will likely be closed soon. – user692942 Nov 17 '16 at 12:44
  • I knew that this code is wrong, but I would like to understand how to execute the same function in buffer. – Vixed Nov 17 '16 at 12:46
  • @Lankymart you're a good ASP dev, so I really think that you understand what I need. Can you help me to better explain my question? I've added a **Note** section – Vixed Nov 17 '16 at 12:49
  • As far as I'm concerned [@reza-aghaei](http://stackoverflow.com/users/3110834/reza-aghaei) has [already provided you with a complete answer](http://stackoverflow.com/a/40637495/692942) so I'll wish you well. – user692942 Nov 17 '16 at 12:51

2 Answers2

2

You can not return a value from a void method:

void Page_Load(Object sender, EventArgs e) 

You just need to write in response. remove the return statement and change the method to:

void Page_Load(Object sender, EventArgs e) 
{
    //...
    var bytes= (byte[])new ImageConverter().ConvertTo(bmPhoto, typeof(byte[]));
    Response.ContentType = "image/bmp";
    Response.OutputStream.Write(bytes, 0, bytes.Length);
}
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • Prefer [@juergen-gutsch suggested approach](http://stackoverflow.com/questions/40654599/error-in-return-variable-as-image#comment68541049_40654599). – user692942 Nov 17 '16 at 12:38
  • @Lankymart See the [previous question](http://stackoverflow.com/questions/40636439/how-can-i-execute-an-aspx-from-url-returning-an-contenttype-image) of OP. I suggested the same. But tihs question is specific to the current problem which they have. – Reza Aghaei Nov 17 '16 at 12:40
  • Yes but you excluded all my code!!!! so just explain me the ** //.. ** part of your answer. – Vixed Nov 17 '16 at 12:58
  • @Vixed That's because the initial question was completely different. You should not edit question to a totally different one when someone answers your question. Surely – Reza Aghaei Nov 17 '16 at 12:59
  • ok but now I've published a new one like you told me to do. So? :/ – Vixed Nov 17 '16 at 12:59
  • 1
    So read the answer!! It's completely what you need, I even can see the result in my machine! – Reza Aghaei Nov 17 '16 at 13:00
  • You want me to cry. Where is the rest of the code?! Can you please just copy and paste it in your answer? kindly – Vixed Nov 17 '16 at 13:02
  • Can you say what's the problem? Dude! just remove the line containing `return` and add those 3 lines at the end of method. – Reza Aghaei Nov 17 '16 at 13:03
  • I'll stay here to hep you solve the problem OK? Just ask your problem about applying the solution and after you solved the problem, just remove the question. – Reza Aghaei Nov 17 '16 at 13:05
  • Unfortunately based on [your offensive comment](http://stackoverflow.com/questions/40654599/error-in-return-variable-as-image#comment68541213_40655065) some users think I made you to post another question just for reputations. This is not the case at all! – Reza Aghaei Nov 17 '16 at 13:06
  • @TheLethalCoder I believe you judged my answer too soon. I know your answer points to the void problem, but for a web page, you need to write to response to get the result. I don't think the answer deserves a downvote. Unfortunately your comment arranged a downvote attack! – Reza Aghaei Nov 17 '16 at 13:18
  • @RezaAghaei I never downvoted however at that point in time my comment was correct. Also I pointed the user in the correct direction for writing to the response stream but as this question only asked to fix the error I deemed it as out of scope so only left a fishing pole not the fish. – TheLethalCoder Nov 17 '16 at 13:21
  • Remove Response.ContentType = "image/bmp"; from your answer so I can accept it because I already have Response.ContentType = "image/jpeg"; in my question. – Vixed Nov 18 '16 at 08:46
  • It's just an example and the answer should contain such line of code. It's like you say add rest of code instead of `//...`. Accepting an answer is your right. The thing that I should say is the answer is what you are looking for :) – Reza Aghaei Nov 18 '16 at 09:05
  • As an additional note, since you are returning exactly a `Bitmap` bytes, the correct content type is `image/bmp`. If you save the file as jpeg and then read its content, you can use `image/jpeg` encoding when returning data. – Reza Aghaei Nov 18 '16 at 09:10
1

It looks to me like you are trying to return a Bitmap object from an event handler that returns void. I.e.

void Page_Load(Object sender, EventArgs e) {

    //...

    using (System.Drawing.Graphics grPhoto = System.Drawing.Graphics.FromImage(bmPhoto)) {

        //...

        return bmPhoto; // <-- HERE
    }
}

To fix this I would set the Bitmap object in a variable somewhere if you want to use it for later user. If you do not need it again just don't return anything from the method.

As @Juergen Gutsch commented you could try writing the image to the response stream.

TheLethalCoder
  • 6,668
  • 6
  • 34
  • 69
  • writing the image to the response stream... ok but how? – Vixed Nov 17 '16 at 12:21
  • @Vixed I do not know asp.net, I was merely posting an idea suggested by another user. Apart from that my answer should be telling you what is causing the exception. – TheLethalCoder Nov 17 '16 at 12:22